模型绑定到列表<&燃气轮机;发布JavaScript数据对象时
我试图发布一个JavaScript数据对象,其中包含以下内容:模型绑定到列表<&燃气轮机;发布JavaScript数据对象时,javascript,jquery,asp.net-mvc,model-binding,Javascript,Jquery,Asp.net Mvc,Model Binding,我试图发布一个JavaScript数据对象,其中包含以下内容: $.post(frm.attr("action"), data, function(res) { // do some stuff }, "json"); 其中“数据”采用 data - panelId - siteId - ConfiguredFactsheetId // this is an array of CheckBox ids that correspond to ConfiguredFactsheets
$.post(frm.attr("action"), data, function(res)
{
// do some stuff
}, "json");
其中“数据”采用
data
- panelId
- siteId
- ConfiguredFactsheetId // this is an array of CheckBox ids that correspond to ConfiguredFactsheets
- 123
- 234
- 345
这样,站点和面板都正确实例化并与它们的数据绑定,但列表对象为空
public JsonResult Edit(Site site, Panel panel, List<ConfiguredFactsheet> configuredFactsheets)
{
// do stuff
return Json(success);
}
但这显然行不通,因为每次我向对象添加新的ConfiguredFactsheetId时,它都会覆盖上一个
我知道,若我构建了表单的查询字符串,我就可以做到这一点
"&ConfiguredFactsheet[i].configuredFactsheetId = " + configuredFactsheetId;
但我希望将所有内容都包含在一个数据对象中
有什么建议吗?我需要更清楚地解释任何事情(可能所有事情!)吗
谢谢
Dave另一种选择是发布:
?myValues=1&myValues=2&myValues=3
把它当作一个数不清的数字
public ActionResult DoSomething(IEnumerable<int> myValues) {
...
public ActionResult DoSomething(IEnumerable MyValue){
...
另一种选择是发布:
?myValues=1&myValues=2&myValues=3
把它当作一个数不清的数字
public ActionResult DoSomething(IEnumerable<int> myValues) {
...
public ActionResult DoSomething(IEnumerable MyValue){
...
最后,这起作用了:
var valuesArray = objCheckBoxes.map(function()
{
return $.getAttributes($(this));
});
var obj = new Array();
$.each(valuesArray, function(item) { obj.push($(this)[0]); });
$.each(obj, function(i)
{
// basically I take the rule where you need to append
// the index to the type, and I apply it here.
data["configuredFactsheets[" + i + "].configuredFactsheetId"] = $(this).attr("configuredFactsheetId");
});
注意:阅读有关的内容最后,这一点起到了作用:
var valuesArray = objCheckBoxes.map(function()
{
return $.getAttributes($(this));
});
var obj = new Array();
$.each(valuesArray, function(item) { obj.push($(this)[0]); });
$.each(obj, function(i)
{
// basically I take the rule where you need to append
// the index to the type, and I apply it here.
data["configuredFactsheets[" + i + "].configuredFactsheetId"] = $(this).attr("configuredFactsheetId");
});
注意:阅读有关AFAIK的信息如果您有一个名为
ConfiguredFactsheet
的数组,那么您应该有参数名“ConfiguredFactsheet。如果它存储整数列表,则它可以是
IEnumerable。因此使用
公共JsonResult Edit(IEnumerable ConfiguredFactsheet)`.panelId如何转换为panel?您有自己的ModelBinder吗?如果您有一个名为ConfiguredFactsheet
的数组,那么您应该有参数名“ConfiguredFactsheet。如果它存储int列表,它可以是
IEnumerable。因此请使用
公共JsonResult编辑(IEnumerable ConfiguredFactsheet)`.panelId如何转换为panel?您有自己的ModelBinder吗?