【发布时间】:2014-02-28 07:40:07
【问题描述】:
我正在开发一个 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; }
}
现在我在客户端有相同的 json 模型,我想发布到服务器。我正在使用以下 jquery ajax:
$.ajax({
url: "address to controller",
type: "post",
data: JSON.stringify(data),
contentType: "application/json",
success: function () {
alert("data saved successfully");
}
});
但只有 PersonModel 属性被映射,但 Contact 属性为空。谁能告诉我我错过了什么??
【问题讨论】:
-
data的内容是什么? -
数据变量的内容是
"{"Contact":{"Address":"xyz","City":"xyz","State":"xyz"},"FirstName":"xyz","LastName":"xyz","Profession":"87"}" -
我发现了问题,问题是我没有给集合;访问我的
PersonModel中的属性Contact,因此默认模型绑定器无法将新对象设置为Contact属性。我刚刚为Contact属性添加了{ get; set; },问题解决了!
标签: c# javascript jquery asp.net-mvc