ASP.NET MVC 1.0 - 字典的模型绑定器
我有一个 ViewModel 类,其中包含一个字典(以及其他与此问题无关的内容):
public class MyViewModel {
public Dictionary<int, string> Data { get; set; }
/* ... */
}
然后我有几个 GET
/POST
操作处理字典。 GET
操作将首先使用数据库中的一些数据填充 Dictionary
,然后返回一个 View
:
>.Key
将位于隐藏字段;并且.Value
将位于文本区域上供用户编辑。
然后,用户将提交此表单,调用 POST
操作。它将尝试处理输入(该过程是不相关的)。一些Key
/Value
对将有效,一些将无效。
如果存在无效对,POST
操作将重建一个 ViewModel,但这次字典应仅包含无效 对,并且将重新显示相同的视图以供用户修复并再次尝试提交。
问题:什么是最简单的?实现这一目标的最干净的方法是什么?
到目前为止,我在控制器中所拥有的(工作正常):
public ActionResult MyAction(MyViewModel vm) {
/* Process data */
if (are there invalid pairs?) {
var vmErrors = new MyViewModel();
/* ... fill the ViewModel ... */
vmErrors.Data =
vm.Data
.Where(x => IsInvalid(x))
.ToDictionary(x => x.Key, x => x.Value);
return View(vmErrors);
}
}
和视图:
<% var i = 0; foreach (var p in Model.Data) { %>
<%= Html.Hidden("vm.Data[" + i + "].key", vm.Key %>
<%= Html.TextArea("vm.Data[" + i + "].value", vm.Value %>
<% i++; } %>
问题是,为了取悦模型绑定器,我必须使用顺序 ID 命名视图上的字段。但我还必须迭代字典以提取键/值对,因此它不能是 for (var i = 0; i < Model.Data.Count; i++) {...} 循环。
I have a ViewModel class containing a Dictionary (and other irrelevant things for this question):
public class MyViewModel {
public Dictionary<int, string> Data { get; set; }
/* ... */
}
Then I have a couple of GET
/POST
actions that deal with the dictionary. The GET
action will first fill the Dictionary<int, string>
with some data from the database, and then return a View
:
- the
.Key
s will be on hidden fields; and - the
.Value
s will be on a textareas for the user to edit.
The user will then submit this form, calling the POST
action. It will try to process the input (the process is irrelevant). Some of the Key
/Value
pairs will be valid, some will be invalid.
If there are invalid pairs, the POST
action will then reconstruct a ViewModel, but this time the dictionary should contain only the invalid pairs, and will redisplay the same View for the user to fix and try submitting again.
Question: what is the easiest & cleanest way to achieve this?
What I have so far (working fine), in the controller:
public ActionResult MyAction(MyViewModel vm) {
/* Process data */
if (are there invalid pairs?) {
var vmErrors = new MyViewModel();
/* ... fill the ViewModel ... */
vmErrors.Data =
vm.Data
.Where(x => IsInvalid(x))
.ToDictionary(x => x.Key, x => x.Value);
return View(vmErrors);
}
}
And the view:
<% var i = 0; foreach (var p in Model.Data) { %>
<%= Html.Hidden("vm.Data[" + i + "].key", vm.Key %>
<%= Html.TextArea("vm.Data[" + i + "].value", vm.Value %>
<% i++; } %>
The problem is that to please the model binder I must name my fields on the view with sequential ids. But I must also iterate over the Dictionary to extract the key/value pairs, so it cannot be a for (var i = 0; i < Model.Data.Count; i++) {...}
loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本文描述了该解决方案 - ASP.NET MVC2 和 MVC3 中的字典模型绑定器
该代码是通用的,也可以与 MVC 1 一起使用。
The solution is described in this article - Dictionary Model Binder in ASP.NET MVC2 and MVC3
The code is general-purpose and could be used with MVC 1 too.