使用多个 ICollection<anotherModel> 创建并发送 JSON 到 MVC 操作
Create and Send JSON to MVC Action with multiple ICollection<anotherModel>
我有一个主从表。我的主实体模型是 Person
,我的细节模型是 ContactInfo
和 AddressInfo
。我的 Person
模型是这样的:
public System.Guid PersonID { get; set; }
public int PersonTypeID { get; set; }
public string PersonFamily { get; set; }
public string PersonName { get; set; }
public Nullable<int> GenderID { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public string Email { get; set; }
public string Description { get; set; }
public virtual ICollection<AddressInfo> AddressInfoes { get; set; }
public virtual ICollection<ContactInfo> ContactInfoes { get; set; }
而且我的 ContactInfo 模型是这样的:
public System.Guid ContactInfoID { get; set; }
public System.Guid PersonID { get; set; }
public int ContactInfoTypeID { get; set; }
public Nullable<System.Guid> CountryDivisionID { get; set; }
public string ContactNumber { get; set; }
public virtual CountryDivision CountryDivision { get; set; }
public virtual Person Person { get; set; }
我从 Master Detail 表单创建我的 JSON 和 Post,如下所示:
var request = $.ajax({
url: actionUrl,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
不幸的是,我从来没有成功构建正确的 JSON 格式来正确投射,而且我的 Person model
总是无法填充 ContactInfos collection
而且我的模型总是没有 ContactInfos
.
我的操作是这样的:
[HttpPost]
public ActionResult Create(Person model)
{
...
}
最后是我的问题;创建动作无法在我的 Person model
中施放 ContactInfos
。
Posted JSON 我的操作收到的是这样的:
{"model":
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"PersonName":"Jack",
"PersonFamily":"Stivenssen",
"GenderID":"1",
"BirthDate":"",
"Email":"d@iv.com",
"Description":"this is a tet",
"ContactInfos":[
{"ContactInfoID":"de2c0de1-9297-4885-9882-44ae210ae6f0",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":3,
"CountryDivisionTitle":"Amity",
"CountryDivisionID":"9fbd3616-47ae-4608-992e-1e2d0a51d2e9",
"IsActive":"false"},
{"ContactInfoID":"7f9a44d3-0532-4976-99e9-017cb59d22cc",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":2,"ContactInfoTypeTitle":"Office",
"CountryDivisionTitle":"Boston",
"CountryDivisionID":"ea0b1a1b-a0c5-46b3-85c1-62bee4d6278e",
{"ContactInfoID":"b3dfe0da-dc0c-48d8-8a07-8438e9149ddd",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":1,
"ContactInfoTypeTitle":"Mobile",
"CountryDivisionTitle":"",
"CountryDivisionID":"",
"IsActive":"false"}
],
"AddressInfos":[]
}
}
正如@George 在评论中指出的那样,您的 AddressInfo 和 ContactInfo 属性中有错字。修复此问题并将它们更改为 IList 而不是 ICollection:
public virtual ICollection<AddressInfo> AddressInfos { get; set; }
public virtual ICollection<ContactInfo> ContactInfos { get; set; }
到
public virtual IList<AddressInfo> AddressInfos { get; set; }
public virtual IList<ContactInfo> ContactInfos { get; set; }
您发布的 JSON 看起来不错,但是您将其包装在 model
属性 中。您需要删除它,这样您 JSON 看起来像:
{
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"PersonName":"Jack",
"PersonFamily":"Stivenssen",
"GenderID":"1",
"BirthDate":"",
"Email":"d@iv.com",
"Description":"this is a tet",
"ContactInfos":[
{"ContactInfoID":"de2c0de1-9297-4885-9882-44ae210ae6f0",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":3,
"CountryDivisionTitle":"Amity",
"CountryDivisionID":"9fbd3616-47ae-4608-992e-1e2d0a51d2e9",
"IsActive":"false"},
{"ContactInfoID":"7f9a44d3-0532-4976-99e9-017cb59d22cc",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":2,"ContactInfoTypeTitle":"Office",
"CountryDivisionTitle":"Boston",
"CountryDivisionID":"ea0b1a1b-a0c5-46b3-85c1-62bee4d6278e",
{"ContactInfoID":"b3dfe0da-dc0c-48d8-8a07-8438e9149ddd",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":1,
"ContactInfoTypeTitle":"Mobile",
"CountryDivisionTitle":"",
"CountryDivisionID":"",
"IsActive":"false"}
],
"AddressInfos":[]
}
感谢@George 和@timothyclifford 分别发表评论和回答。
不幸的是,答案太简单了,我花了两天时间才找到。
包括我在内的任何人都没有注意到我的 Person
模型中 ContactInfo
集合的名称是:ContactInfoes
最后是 es 而不是 ContactInfos
.
是的,伙计们,问题是语法不匹配,也不是 IList<>
属性 类型,也不是 {"model"
名称出现在 JSON 的开头,没有冒犯。(我花了两天时间发现这个荒谬的问题,如果你处在我的位置,肯定会发现它比我更快。;) )
我有一个主从表。我的主实体模型是 Person
,我的细节模型是 ContactInfo
和 AddressInfo
。我的 Person
模型是这样的:
public System.Guid PersonID { get; set; }
public int PersonTypeID { get; set; }
public string PersonFamily { get; set; }
public string PersonName { get; set; }
public Nullable<int> GenderID { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public string Email { get; set; }
public string Description { get; set; }
public virtual ICollection<AddressInfo> AddressInfoes { get; set; }
public virtual ICollection<ContactInfo> ContactInfoes { get; set; }
而且我的 ContactInfo 模型是这样的:
public System.Guid ContactInfoID { get; set; }
public System.Guid PersonID { get; set; }
public int ContactInfoTypeID { get; set; }
public Nullable<System.Guid> CountryDivisionID { get; set; }
public string ContactNumber { get; set; }
public virtual CountryDivision CountryDivision { get; set; }
public virtual Person Person { get; set; }
我从 Master Detail 表单创建我的 JSON 和 Post,如下所示:
var request = $.ajax({
url: actionUrl,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
不幸的是,我从来没有成功构建正确的 JSON 格式来正确投射,而且我的 Person model
总是无法填充 ContactInfos collection
而且我的模型总是没有 ContactInfos
.
我的操作是这样的:
[HttpPost]
public ActionResult Create(Person model)
{
...
}
最后是我的问题;创建动作无法在我的 Person model
中施放 ContactInfos
。
Posted JSON 我的操作收到的是这样的:
{"model":
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"PersonName":"Jack",
"PersonFamily":"Stivenssen",
"GenderID":"1",
"BirthDate":"",
"Email":"d@iv.com",
"Description":"this is a tet",
"ContactInfos":[
{"ContactInfoID":"de2c0de1-9297-4885-9882-44ae210ae6f0",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":3,
"CountryDivisionTitle":"Amity",
"CountryDivisionID":"9fbd3616-47ae-4608-992e-1e2d0a51d2e9",
"IsActive":"false"},
{"ContactInfoID":"7f9a44d3-0532-4976-99e9-017cb59d22cc",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":2,"ContactInfoTypeTitle":"Office",
"CountryDivisionTitle":"Boston",
"CountryDivisionID":"ea0b1a1b-a0c5-46b3-85c1-62bee4d6278e",
{"ContactInfoID":"b3dfe0da-dc0c-48d8-8a07-8438e9149ddd",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":1,
"ContactInfoTypeTitle":"Mobile",
"CountryDivisionTitle":"",
"CountryDivisionID":"",
"IsActive":"false"}
],
"AddressInfos":[]
}
}
正如@George 在评论中指出的那样,您的 AddressInfo 和 ContactInfo 属性中有错字。修复此问题并将它们更改为 IList 而不是 ICollection:
public virtual ICollection<AddressInfo> AddressInfos { get; set; }
public virtual ICollection<ContactInfo> ContactInfos { get; set; }
到
public virtual IList<AddressInfo> AddressInfos { get; set; }
public virtual IList<ContactInfo> ContactInfos { get; set; }
您发布的 JSON 看起来不错,但是您将其包装在 model
属性 中。您需要删除它,这样您 JSON 看起来像:
{
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"PersonName":"Jack",
"PersonFamily":"Stivenssen",
"GenderID":"1",
"BirthDate":"",
"Email":"d@iv.com",
"Description":"this is a tet",
"ContactInfos":[
{"ContactInfoID":"de2c0de1-9297-4885-9882-44ae210ae6f0",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":3,
"CountryDivisionTitle":"Amity",
"CountryDivisionID":"9fbd3616-47ae-4608-992e-1e2d0a51d2e9",
"IsActive":"false"},
{"ContactInfoID":"7f9a44d3-0532-4976-99e9-017cb59d22cc",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":2,"ContactInfoTypeTitle":"Office",
"CountryDivisionTitle":"Boston",
"CountryDivisionID":"ea0b1a1b-a0c5-46b3-85c1-62bee4d6278e",
{"ContactInfoID":"b3dfe0da-dc0c-48d8-8a07-8438e9149ddd",
"PersonID":"e715b7a2-d349-4239-af12-c9037bdf4bee",
"ContactInfoTypeID":1,
"ContactInfoTypeTitle":"Mobile",
"CountryDivisionTitle":"",
"CountryDivisionID":"",
"IsActive":"false"}
],
"AddressInfos":[]
}
感谢@George 和@timothyclifford 分别发表评论和回答。
不幸的是,答案太简单了,我花了两天时间才找到。
包括我在内的任何人都没有注意到我的 Person
模型中 ContactInfo
集合的名称是:ContactInfoes
最后是 es 而不是 ContactInfos
.
是的,伙计们,问题是语法不匹配,也不是 IList<>
属性 类型,也不是 {"model"
名称出现在 JSON 的开头,没有冒犯。(我花了两天时间发现这个荒谬的问题,如果你处在我的位置,肯定会发现它比我更快。;) )