[英]Ajax calling MVC action method - No parameterless constructor defined for this object
I'm making the following AJAX call to an action method in an ASP.NET MVC controller: 我正在对ASP.NET MVC控制器中的操作方法进行以下AJAX调用:
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: appControllerUrl + "/SaveAppDetails",
data: {appDetails: JSON.stringify(appDetailsView.model)},
processData: true,
dataType: "json"
});
However I get an HTTP 500 error stating 'No parameterless constructor defined for this object'. 但是我收到一个HTTP 500错误,指出“没有为此对象定义无参数构造函数”。 Here is my controller:
这是我的控制器:
public class AppDetailsController
{
// GET: AppDetails
AppDetailsController() { }
[HttpPost]
public ActionResult SaveAppDetails(string appDetails)
{
...
}
The 'data' that the Ajax call passes to the controller looks this this: Ajax调用传递给控制器的“数据”看起来像这样:
appDetails= {"id":{"type":"string"},"appName":{"type":"string"},"guid":21}
... so it looks like the actual data I need isn't being passed, however the data string itself seems to be well formed. ...因此看起来好像我没有传递实际所需的数据,但是数据字符串本身似乎格式正确。 What's causing the error?
是什么导致错误?
The error means that MVC is trying to instantiate a controller but cannot do so. 该错误表示MVC试图实例化控制器,但无法这样做。 In C#, if you declare a constructor and don't specify an accessibility modifier, it will be
private
. 在C#中,如果您声明一个构造函数而不指定可访问性修饰符,则它将是
private
。 You should add the public
modifier to the constructor of AppDetailsController
. 您应该将
public
修饰符添加到AppDetailsController
的构造函数中。
Javascript Example Javascript范例
appDetailsView.model = {
Id: $.trim(idExample),
AppName: $.trim(appNameExample),
Guid: 21
};
Ajax 阿贾克斯
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: appControllerUrl + "/SaveAppDetails",
data: JSON.stringify(appDetailsView.model),
processData: true,
dataType: "json"
});
Action 行动
public class AppDetailsController
{
// GET: AppDetails
public AppDetailsController() { }
[HttpPost]
public ActionResult SaveAppDetails(AppDetailsView appDetails)
{
...
}
Class View Model 类视图模型
public class AppDetailsView{
public string Id { get; set; }
public string AppName { get; set; }
public int Guid { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.