我试图发布具有以下一个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
   - 123
   - 234
   - 345

通过此,这两个位点面板被正确地实例化和与它们结合的数据,但在列表对象为空。

public JsonResult Edit(Site site, Panel panel, List<ConfiguredFactsheet> configuredFactsheets)
{
    // do stuff
    return Json(success);
}

现在,我知道我的“数据”对象的ConfiguredFactsheetId属性只是ID值的阵列。我需要指定每个值对应于我ConfiguredFactsheet对象的configuredFactsheetId财产?如果是这样,我的数据对象将采取形式similart到

data
 - panelId
 - siteId
 - ConfiguredFactsheet // this is an array of CheckBox ids that correspond to ConfiguredFactsheets
   - ConfiguredFactsheetId:123
   - ConfiguredFactsheetId:234
   - ConfiguredFactsheetId:345

但是这显然是行不通的,因为我每次添加一个新的ConfiguredFactsheetId对象时,它只会覆盖前一个。

我知道我能做到这一点,如果我建形式的查询字符串

"&ConfiguredFactsheet[i].configuredFactsheetId = " + 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");
});

注意:阅读有关 $ .getAttributes

其他提示

另一种方法是张贴:

?myValues=1&myValues=2&myValues=3

和接受它作为一个IEnumerable

public ActionResult DoSomething(IEnumerable<int> myValues) {
    ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top