简体   繁体   English

MVC模型绑定无法按预期在AJAX请求中的复杂类型上正常工作

[英]MVC Model binding not working as expected on complex type from AJAX request

I have a 'ViewModel' I want to pass to my action, and it looks like this: 我有一个要传递给我的操作的“ ViewModel”,它看起来像这样:

public class MyClass
{
    public int Id { get; set; }
    public int MyNumber { get; set; }
}

public class RequestViewModel
{
    public List<MyClass> MyClasses { get; set; }
    public int AnotherNumber { get; set; }
}

In javascript, I define my data like so: 在javascript中,我定义数据的方式如下:

       var actionRequest = {
            MyClasses: [
                {
                    Id: 1,
                    MyNumber: 30
                },
                {
                    Id: 2,
                    MyNumber: 40,
                }
            ],
            AnotherNumber: 12
        };

I make the call: 我打了电话:

    $.ajax({
        type: "Get",
        url: " /Controller/MyAction",
        data: actionRequest,
        dataType: "json",
        traditional: true,
        success: function (data) {
            // Good stuff
        },
        error: function (data) {
            // Bad stuff
        }
    });

My action's signature: 我动作的签名:

public ActionResult MyAction(RequestViewModel request)

On inspecting request (my action is successfully hit), AnotherNumber is populated whereas my collection of MyClass is null . 在检查request (成功执行了我的操作),填充AnotherNumber ,而我的MyClass集合为null

I'm missing something obvious here, I just don't know what. 我在这里缺少明显的东西,我只是不知道。

EDIT 编辑

I really do appreciate the suggestions and answers so far but none (upto now) have worked, so for now I'm settling for: 到目前为止,我确实非常感谢您的建议和答案,但是到目前为止(到目前为止)都没有任何效果,因此,我现在争取:

public class RequestViewModel
{
    public int[] MyClassIds { get; set; }
    public int[] MyNumberValues { get; set; }
    public int AnotherNumber { get; set; }
}

It feels horrible doing this, but it's a project that has quite a bit of urgency behind it now so I have to go with what works. 这样做感觉很恐怖,但这是一个项目,它现在有很多紧迫性,因此我必须遵循可行的方法。

try below code and put the name same as it is in controller as shown below :- 尝试下面的代码,并将其名称与控制器中的名称相同,如下所示:-

$.ajax({
        type: "POST",
        url: " /Controller/MyAction",
        data: :{ request : JSON.stringify(actionRequest) },
        dataType: "json",
        traditional: true,
        success: function (data) {
            // Good stuff
        },
        error: function (data) {
            // Bad stuff
        }
    });

and I would suggest try POST request put httpPost attribute above action method as shown below :- 我建议尝试POST请求,将httpPost属性放在操作方法上方,如下所示:-

[HttpPost]
public ActionResult MyAction(RequestViewModel request)

Try using JSON.stringify and modify data in ajax request as : 尝试使用JSON.stringify并将ajax请求中的data修改为:

data:{ request : JSON.stringify(actionRequest) }

and add contentType: "application/json" in your ajax request. 并在ajax请求中添加contentType: "application/json"


OR Try This : 或尝试一下:

var request = {
        MyClasses: [
            {
                Id: 1,
                MyNumber: 30
            },
            {
                Id: 2,
                MyNumber: 40,
            }
        ],
        AnotherNumber: 12
    };

 request = JSON.stringify({ 'request': request });

now use data in ajax request this way : 现在以这种方式在ajax请求中使用data

data: request 

I'm not sure where the difference is and now I can't reproduce the error but this works for me. 我不确定区别在哪里,现在我无法重现错误,但这对我有用。

public class MyClass
{
    public int Id { get; set; }
    public int MyNumber { get; set; }
}

public class RequestViewModel
{
    public List<MyClass> MyClasses { get; set; }
    public int AnotherNumber { get; set; }
}

[HttpPost]
public ActionResult Save(RequestViewModel actionRequestX)
{
        return null;
}

var actionRequest = {
        MyClasses: [
            {
                Id: 1,
                MyNumber: 30
            },
            {
                Id: 2,
                MyNumber: 40
            }
        ],
        AnotherNumber: 12
    };

    $.ajax({
    type: "POST",
    url: "/Home/Save",
    contentType: "application/json, charset=utf-8",
    data: JSON.stringify({ actionRequestX: actionRequest }),
    dataType: "json",
    traditional: true,
    success: function (data) {
        // Good stuff
    },
    error: function (data) {
        // Bad stuff
    }
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM