Error : Third party action method not found - javascript

I am using one third party tool. I have inherited its controller with MVC controller
{
public class ReportController : ReportServiceBaseController
{
}
public abstract class ReportServiceBaseController : Controller
{
[HttpPost]
public JsonResult LoadDocumentInfo(LoadDocumentInfoRequest request);
[HttpPost]
public JsonResult LoadDocumentMapInfo(LoadDocumentMapInfoRequest request);
}
}
When I make call to "LoadDocumentInfo" method it give an error of not found.
Please help.

you use ReportController to call LoadDocumentInfo method ,but you have not implement this method in Controller ,so i think first you have to implement it in your controller and than try to call that method.

Related

#Url.Action is null or "" in javascript when page loads

I have this function located in a controller called AccountCodes.
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> Process()
{
JsonResult result = new JsonResult(null);
List<string> uploadResults = new List<string>();
return Ok(uploadResults);
}
My intention is to use dropzone to push files to this function. When I try to load the url in the razor view in a variable like this:
var url = "#Url.Action("Process", "AccountCodes",new { area="Configurations"})";
However, when I load the page and look at the console, the url is always "". Am I missing something?
EDIT:
This is where I put the javascript code:
$(document).ready(function () {
var url = "#Url.Action("Process", "AccountCodes",new { area="Configurations"})";
$("#reset").attr("disabled", true);
});
Your code is correct. It may because that you have some wrong global configuration.
Please verify the following things:
Step 1: Check your application
Use MVC in your start up:
// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other middlewares.
app.UseMvcWithDefaultRoute();
}
Step 2: Check your controller
Make sure there is a controller named AccountCodesController.
Make sure AccountCodesController extends Controller.
Check your routes. If you specify [Route("Something")] to your controller, make sure it is absolutely correct.
Step 3: Try adding a route to your action.
Adding an attribute [Route('SomeAction')] to your action Process might solves this.
Step 4: Check your area existance
Please make sure your controller is located in correct area as this document mentioned: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-2.2#areas-for-controllers-with-views .
namespace MVCareas.Areas.Products.Controllers
{
[Area("Products")]
public class ManageController : Controller

How to configure common data model across android and JS bridge?

I have a JS bridge in Android,
public class WebInterface {
Context mContext;
public WebInterface(Context c) { this.mContext = c;}
#JavascriptInterface
public void showAlert() {
Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();
}
}
I can set interface in my webView,
mWebView.addJavascriptInterface(WebInterface(this), "android");
This works fine for simple methods like showAlert() where the is no params when the params or the param is a simple string but when I need to pass a data model as a params while calling native functions from web app how can I bind data models? I am required to call implement a function with param of type custom data model.
public class WebInterface {
Context mContext;
public WebInterface(Context c) { this.mContext = c;}
#JavascriptInterface
public void showAlert() {
Toast.makeText(mContext, "This is being called from the interface", Toast.LENGTH_SHORT).show();
public void saveData(data: DataModel) { // DataModel is custom model
Toast.makeText(mContext, "Saving data model", Toast.LENGTH_SHORT).show();
}
}
How can I bind data model across native and web app. Is it possible using TypeScript? If so, how to configure? Is it only possible using plain json string as params? no any other way?
You should use the JSON string.
You can create another function receive the format you want, then JSON.stringify the object before pass to the function.
Javascript
function saveData(obj){
const json = JSON.stringify(obj);
Android.saveData(json);
}
Alternative away is used to JSON.parse(data)
javascript
function loadJson(obj) {
var jsonValue = JSON.parse(obj)
Android.saveData(jsonValue)
}

How can I handle a dynamic viewModel in my controller?

In my controller, my create method takes a viewModel that inherits from DynamicObject. The reason is that I need static properties on the model, but I may also add things to it via JavaScript before I pass it to the controller.
So, I have:
public class MyViewModel: DynamicObject {
public string id {get;set;}
}
[HttpPost]
public IHttpActionResult Create(MyViewModel viewModel) {
// save to Db
}
From the JavaScript (using Angular), I've passed in this viewmodel:
var vm = { id: 1, name: "Jim"};
However, this does not work and is throwing me this error:
[{"PropertyName":null,"ErrorMessage":null,"ErrorCode":null,"AttemptedValue":null,"CustomState":null}]
I haven't found an example of what I'm trying to do, but I would think making an object dynamic would allow for this. What am I missing here?

How to bind a complex data structure to Spring Controller

I am trying to bind a complex data structure to my controller in spring mvc with javascript.
I have this ViewModel;
public class LibraryViewModel{
private String idLibrary;
private String nameLibrary;
private List<Box> listBox;
//Getters and setters
}
And this is the box's class:
List listBox;
public class Box{
private String idBox;
private List<String> booksNames;
//Getters and setters
}
What I want to do?
I want to do the form binding idLibrary and nameLibrary with a form in jsp
<form:form modelAttribute = "libraryVM" id="bestForm".../>
And listBox get a data injection like with a function made in JavaScript.
$('#bestForm').submit(function() {
completeListBoxInfo();
$('#organizacion').val(a);
});
But I allways get null mi List in mi controller but I get fine my idLibrary and nameLibrary attributes.
#RequestMapping("/save")
protected void guardar(#RequestParam("libraryFM") LibraryViewModel newLibrary) {
...
}
How can I bind the View Model object with the function result of completeListBoxInfo();
Kind regards.

Call Action in Controller From View and send parameter

I am trying to call a method inside a controller in MVC from a javascript action. The javascript action is supposed to invoke this method inside the controller and send some parameters to it.
My Javascript code looks like this:
location.href = '#Url.Content("~/Areas/MyArea/MyMethod/"+Model.MyId)';
My Method is defined as follows:
[HttpGet]
public ActionResult MyMethod(int? MyId)
{
doSomething(MyId);
return View("MyView");
}
However, when i debug the application, when the method is called the MyId parameter is passed as null and not as the current value of the MyId parameter in my model. What can I do to correctly send or retrieve this value? Thanks!
In your route definition I suppose that the parameter is called {id} and not {MyId}:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
So try to be more consistent and adapt your controller action parameter name accordingly:
[HttpGet]
public ActionResult MyMethod(int? id)
{
doSomething(id);
return View("MyView");
}
Also you probably wanna use url helpers instead of hardcoding some url patterns in your javascript code:
window.location.href = '#Url.Action("MyMethod", "SomeControllerName", new { area = "MyArea", id = Model.MyId })';
The Url.Content helper is used to reference static resources in your site such as javascript, css and image files. For controller actions it's much better to use the Url.Action helper method.

Categories