模型绑定到列表<>发布JavaScript数据对象时 [英] Model Bind to a List<> when Posting JavaScript Data Object

查看:108
本文介绍了模型绑定到列表<>发布JavaScript数据对象时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下内容发布JavaScript数据对象:

I'm trying to post a JavaScript data object with the following:

$.post(frm.attr("action"), data, function(res)
{
    // do some stuff
}, "json");

其中'data'采用

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

有了这个,网站和&面板正确实例化&绑定了他们的数据,但List对象为null。

With this, both site & panel are correctly instantiated & bound with their data but the List object is null.

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

现在,我意识到我的'data'对象的ConfiguredFactsheetId属性只是一个数组id值。我是否需要指定每个值对应于ConfiguredFactsheet对象的configuredFactsheetId属性?如果是这样,我的数据对象将采用类似的形式

Now, I realise that my 'data' object's ConfiguredFactsheetId property is just an array of id values. Do I need to specify that each value corresponds to a configuredFactsheetId property of my ConfiguredFactsheet object? If so, my data object would take a form similart to

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

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

but this obviously won't work because every time I add a new ConfiguredFactsheetId to the object, it'll just overwrite the previous one.

我知道如果我构建了一个表格的查询字符串,我可以这样做

I know I can do this if I built a query string of the form

"&ConfiguredFactsheet[i].configuredFactsheetId = " + configuredFactsheetId;

但我想在一个数据对象中包含所有内容

but I'd like to contain everything in a single data object

有什么建议吗?我是否需要更清楚地解释任何事情(可能是一切!)

Any suggestions? Do I need to explain anything (probably everything!) more clearly?

谢谢

戴夫

推荐答案

最后,这有效:

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

这篇关于模型绑定到列表&lt;&gt;发布JavaScript数据对象时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆