English 中文(简体)
我该把这种刺绣阵列绑在什么 mvc 对象上?
原标题:To what mvc object do I bind this kind of javascript array?

我用一个[int,bool]来充斥一个阵列:

$.each(ownedChecked, function (key, val) {
    /*code to set id as int and paintedTrue as bool*/
    ownedIDs.push([id,paintedTrue]);
});

我创造了一个变体, 它将被细化:

var saveData = { OwnedListEntryIDs:ownedIDs };

//send data
$.ajax({
    url:  /ListEntry/OwnedModels ,
    type:  POST ,
    contentType:  application/json; charset=utf-8 ,
    data: JSON.stringify(saveData),
    success: function (data) { alert( Update success:   + data); },
    failure: function (data) { alert( Update failed:   + data); }
});

以下是视图模式 :

public class OwnedPaintedSave
{
    //collection contains a ListEntryID and a bool indicating whether it is painted or not
    public Dictionary<int,bool> OwnedListEntryIDs { get; set; }
}

一旦进入控制器方法, 模式状态。 问题通行证和代码就会进入前方循环, 但模型总是 NULL :

[HttpPost]
public JsonResult OwnedModels(OwnedPaintedSave models)
{
    if (ModelState.IsValid)
    {
        foreach (var id in models.OwnedListEntryIDs)
        { }
    }
}

我尝试了设定传统设置: 使用$. ajax 方法, 但问题是一样的。 我猜 ViewModel 中的字典不是我需要的, 但我不确定它应该是什么。 感谢您的帮助 。

最佳回答

我给拥有式纸面保存器 提供了两英寸的属性, 一个是拥有式纸面保存器, 一个是油漆式纸面保存器。

问题回答

我认为问题在于您的控制器。 您正在通过 var saveData = { ownListintryIDs: 拥有IDs}; (基本上是一个 dictionary< int, bull> - yes?) 的数据, 但动作结果中的参数属于类型 自定义的保存

我认为,如果您将您的控制器参数更改为 dictionary<int, bul> 拥有的ListEntryIDs , 并且不将 javascript 对象字符串化, 这个框架就足够聪明, 足以将您的对象序列化和脱序。 所以您将会拥有这个...

var saveData = { OwnedListEntryIDs:ownedIDs };

//send data
$.ajax({
    url:  /ListEntry/OwnedModels ,
    type:  POST ,
    contentType:  application/json; charset=utf-8 ,
    data: saveData,
    success: function (data) { alert( Update success:   + data); },
    failure: function (data) { alert( Update failed:   + data); }
});

然后这个在你的控制器里...

[HttpPost]
public JsonResult OwnedModels(Dictionary<int, bool> ownedListEntryIDs)
{
    if (ModelState.IsValid)
    {
        foreach (var id in ownedListEntryIDs)
        { }
    }
}




相关问题
Getting rid of Microsoft AJAX

We wrote a couple of controls using Microsoft AJAX (cs class + js class). Now I m thinking about getting rid of it (just use jQuery), as it is bloated and we don t use UpdatePanel. My question is: how ...

ASP.NET Ajax 4 and Microsoft Ajax library definitions

I undestand that is former for Web Forms ASP.Net only , latter ( currently preview 6 ) suitable for both - classic and MVC. Would it be possible to give a brief description/usage for each. By ASP.NET ...

ASP.NET Rich UI - What do you use? [closed]

I m looking for feedback on what other people use on their ASP.NET projects to provide a rich user interface experience while still remaining as productive as possible. I m developing an ASP.NET 3.5 ...

CollapsiblePanelExtender collapses on link hover

I am trying to use an CollapsiblePanelExtender to make some dropdown menus and am having some problems when hovering over links - can anyone help me out... By the way I do want to keep the ...