JSON.stringify()ModelBinding作为问题标题所暗示我试图通过<code>$ajax</code>调用一个MVC控制器阵列后的集合(数组的数组,如果你喜欢)绑定到一类实体返回null模型

控制器。我已经触轮附近的所有我能找到的关于该主题的问题,但我似乎无法得到它的工作,模型根本不会绑定。

虽然这将是很好能够绑定到一类实体,我会满足于仅仅能够处理“数组的数组”,但即使这似乎并没有工作。我无法将数组读取为控制器参数中的'数组'。

的追求

的HTML页面用的变量,但潜在的大量的复选框(> 500)需要与每个复选框两个唯一的引用发送的所有值(假值包括在内)回控制器用于处理。这两个唯一的引用是复选框的ID的一部分,并在添加到数组之前分开。

问题通过$ajax呼叫从MVC视图发帖时

,数据被字符串化(通过JSON.stringify())的控制器响应并初始化chkBox实体类的适当数量,但不填充类实例的属性。 (即我得到n个chkBox实体的所有设置为null所有属性

提琴手验证值才能发布,以JSON格式,如果我写我自己我ModelBinder的价值观做流中显示出来,所以数值实际上获得通过到控制器

如果我将一组三个单独的值传递给控制器​​,则会发生同样的情况,但是如果我将三组值作为单个数组传递,则控制器将以单个chkBox实体响应,但全部三个物业值设置为null

JAV ascript函数创建JS 'chkBox对象'

function chkBox(unqReference_01, unqReference_02, boolean) { 

this.unqReference_01 = unqReference_01;

this.unqReference_02= unqReference_02;

this.IsChecked = boolean;

}

的Javascript鉴于

var _chkBoxes = []; 

$("input:checkbox[name^='chkCompetitor']").each(function() {

if (_chkBoxes.length < 5) {

var splitID = $(this).attr('id').split("_");

var entry = new chkBox(splitID[1], splitID[2], true);

if (_chkBoxes.indexOf(entry) == -1) {

_chkBoxes.push(entry);

}

}

});

//Submit _chkBoxes

var updateMatrix = $.ajax({

type: "POST",

dataType: 'json',

contentType: 'application/json; charset=utf-8',

//traditional: true,

url: '/Processing/updateMatrix',

data: JSON.stringify(_chkBoxes)

});

原类:(在所产生的不正确的行为之一)

[SerializableAttribute] 

public class chkBox

{

String unqReference_01 { get; set; }

String unqReference_02 { get; set; }

String IsChecked { get; set; }

public chkBox()

{

}

}


编辑:解决方案(太简单了)

将所有类属性设置为public ...

编辑后的类别:

[SerializableAttribute] 

public class chkBox

{

public String unqReference_01 { get; set; }

public String unqReference_02 { get; set; }

public String IsChecked { get; set; }

public chkBox()

{

}

}


控制器:

[HttpPost] 

public JsonResult UpdateMatrix(General.chkBox[] chkBoxes)

//public JsonResult UpdateMatrix(IEnumerable<General.chkBox> chkBoxes)

//public JsonResult UpdateMatrix(string IsChecked, String unqReference_01, String unqReference_01)

{

return null;

}

使用:

  • ASP.NET MVC 5.2.3
  • EF 6.2.0
  • 的jQuery 3.2.1

阅读&尝试:

  • MVC3 & JSON.stringify() ModelBinding returns null model
  • ASP.NET MVC3 JSON Model-binding with nested class
  • ModelBindingContext.ValueProvider.GetValue(bindingContext.ModelName) returns null
  • ASP.NET MVC3 JSON Model-binding with nested class
  • ModelBinding not deserializing json
  • Post JSON array to mvc controller
  • https://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/

头截图

请注意,标题使用类&属性的实际名称,属性相匹配两端(即在JSON和类) uBoatKey = unqReference_01 uDivisionRaceKey = unqReference_02

回答:

@BrianRogers了最有用的评论,该类具有声明为 '只是'

String unqReference_01 {get; set;}

其属性这应该显然是

public String unqReference_01 {get; set;}

欢迎前往@BrianRogers提供评论。

以上是 JSON.stringify()ModelBinding作为问题标题所暗示我试图通过&lt;code&gt;$ajax&lt;/code&gt;调用一个MVC控制器阵列后的集合(数组的数组,如果你喜欢)绑定到一类实体返回null模型 的全部内容, 来源链接: utcz.com/qa/266335.html

回到顶部