简体   繁体   English

Ajax调用MVC操作方法-没有为此对象定义无参数构造函数

[英]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.

相关问题 在mvc中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in mvc 在MVC中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in MVC 没有为此对象MVC4定义无参数的构造函数 - No parameterless constructor defined for this object MVC4 没有为此对象定义无参数构造函数-MVC项目 - No parameterless constructor defined for this object - MVC project 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object @ Html.Action导致“未为此对象定义无参数构造函数”异常 - @Html.Action causing 'No parameterless constructor defined for this object' exception 在ASP.NET MVC中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in ASP.NET MVC 没有在MVc5回购模式中为此对象定义无参数构造函数 - No parameterless constructor defined for this object in MVc5 Repo pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM