模型绑定到列表>>发布 JavaScript 数据对象时

发布于 2024-08-24 11:50:06 字数 1264 浏览 6 评论 0原文

我正在尝试发布一个 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

通过此,站点和站点”。面板已正确实例化并且与其数据绑定,但 List 对象为 null。

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

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

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;

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

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

谢谢

戴夫

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

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

where 'data' takes the structure of

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

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);
}

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

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?

Thanks

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2024-08-31 11:50:06

最后,这有效:

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

In the end, this worked:

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");
});

Note: read about $.getAttributes

甜扑 2024-08-31 11:50:06

另一种方法是发布:

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

并接受它作为 IEnumerable

public ActionResult DoSomething(IEnumerable<int> myValues) {
    ...

An alternative is to post:

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

And accept it as an IEnumerable

public ActionResult DoSomething(IEnumerable<int> myValues) {
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文