我在将数组发布到控制器时遇到问题。我发送了一个项目列表,在项目中有一组组。尽管发布的sendData变量我有数组,但数组值在控制器中显示为null。
这是我输入的数据。在这种情况下,可变数据为"[{"id":"123456789","firstName":"John","lastName":"Doe","UserGroups":["1","2"]}]"
我的用户模型:
[Display(Name = "SSO")]
[Required]
public virtual string ID { get; set; }
[Display(Name = "First Name")]
[Required]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required]
public string LastName { get; set; }
[Display(Name = "User Group(s)")]
public UserGroupModels[] UserGroup { get; set; }
群组模型。您会注意到一对多关系
public class UserGroupModels
{
public int ID { get; set; }
public string MAMUserGroup { get; set; }
}
AJAX电话。你会注意到我也在使用KnockoutJS
self.save = function (form) {
sendData = ko.toJSON(self.users);//value is "[{"id":"123456789","firstName":"John","lastName":"Doe","UserGroups":["1","2"]}]"
$.ajax({
url: '/Users/CreateMultiple',
contentType: 'application/json',
async: true,
traditional: true,
type: 'POST',
dataType: 'json',
data: sendData,
error: function (jqXHR, textStatus, errorThrown) {
console.log("FAIL: ");
alert("ERROR: " + errorThrown)
hidePleaseWait();
},
success: function (response) {
hidePleaseWait();
console.log(response.alreadyExistList);
这是我的控制器
public async Task<ActionResult> CreateMultiple(List<User> users)
{do something...}
当我查看List<User> user
的本地人时,我看到数组中的UserGroup数量正确(本例中为2),但每个值都为空。例如users[0].UserGroups[0]=null
,users[0].UserGroups[1]=null
答案 0 :(得分:0)
UserGroups json数组中的项应该是对UserGroupModels类进行建模的对象。
"UserGroups":[{"ID":"1"},{"ID":"2"}]