javascript 如何使用ajax将嵌套的json对象发送到mvc控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21550745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:08:30  来源:igfitidea点击:

How to send nested json object to mvc controller using ajax

c#javascriptjqueryasp.net-mvc

提问by user1740381

I am working on an ASP.NET MVC application. I have the following view model in c#:

我正在开发一个 ASP.NET MVC 应用程序。我在 C# 中有以下视图模型:

public class ContactModel
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class PersonModel
{
    public ContactModel Contact;

    public PersonModel()
    {
        Contact = new ContactModel();
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Profession { get; set; }
}

Now i have the same json model at client side which i want to post to server. I am using following jquery ajax:

现在我在客户端有相同的 json 模型,我想发布到服务器。我正在使用以下 jquery ajax:

$.ajax({
    url: "address to controller",
    type: "post",
    data: JSON.stringify(data),
    contentType: "application/json",
    success: function () {
        alert("data saved successfully");
    }
});

But only PersonModel properties are get mapped but Contact properties are null. Can anybody please tell me what i am missing??

但只有 PersonModel 属性被映射,而 Contact 属性为空。有人可以告诉我我错过了什么吗??

回答by ramiramilu

You need for format your string to proper json -

您需要将字符串格式化为正确的 json -

Say if you model is -

假设你的模型是 -

public class ContactModel
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class PersonModel
{
    public ContactModel Contact { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Profession { get; set; }
}

Then you AJAX Post should be like this -

那么你的 AJAX Post 应该是这样的——

<script>
    $(function () {
        $('#click1').click(function (e) {

            var studentData = {
                "FirstName": "Rami",
                "LastName": "Vemula" ,
                "Contact": { "City": "Hyd"}
            };

            $.ajax({
                url: "@Url.Action("Submit")",
                type: "POST",
                data: JSON.stringify(studentData),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (response) {
                    alert(response.responseText);
            },
                success: function (response) {
                    alert(response);
                }
            });

        });
    });
</script>

Then output is going to be -

然后输出将是 -

enter image description here

在此处输入图片说明

回答by Anjo

Create a instance of ContactModel and assign it to contact under PersonModel after creating instance of PersonModel. Please let me know in case of any clarification needed

创建一个ContactModel实例,创​​建PersonModel实例后,将其分配给PersonModel下的contact。如果需要澄清,请告诉我

回答by Pankaj Kumar

If you are using @htmlhelper for properties then form.serialize()method will bind all the properties otherwise if you are using html elements like <input>the assign their name property same as model property.

如果您@html对属性使用帮助程序,则form.serialize()方法将绑定所有属性,否则如果您使用 html 元素,例如<input>将其名称属性分配为与模型属性相同。

<input type="text" name="Contact.FirstName" value="@Model.Contact.FirstName"/>