Model将 ViewModel 列表与 MvcContrib 网格绑定
我正在尝试使用 MvcContrib Grid 帮助器将模型对象列表绑定到网格。显然,发出 HTML 表很容易,但我很难返回所有选定的行(或所有行并通过Where(x => x.Selected) 进行过滤)。
这是我的意思的虚拟版本:
模型:
public class Player
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public int JerseyNumber { get; set; }
public string Position { get; set; }
[ScaffoldColumn(false)]
public bool Selected { get; set; }
}
视图:
@model democode.Models.Player
@using (Html.BeginForm())
{
@{
var grid = Html.Grid(Model)
.AutoGenerateColumns()
.Columns(c => c.For(p => Html.CheckBoxFor(_ => p.Selected)).InsertAt(0))
.Columns(c => c.For(p => Html.HiddenFor(_ => p.Id)))
grid.Render();
}
<p>
<input type="submit" value="Submit" />
</p>
}
因此,您看到的是曲棍球运动员的网格,每个运动员之前都有一个复选框,允许用户选择一个或多个。单击“提交”后,我希望它将集合发回(了解除 Selected 和 Id 之外的所有内容都将为空/默认),但我知道问题在于 POST 数据中遇到的记录在键值对列表。 我过去通过手写 HTML 表并使用 Phil Haack 在此概述的策略成功解决了这个问题:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
我的问题是,可以我使用 MvcContrib 中的网格助手做同样的事情,或者它是否比它的价值更多的工作?
I'm trying to bind a list of model objects to a grid using the MvcContrib Grid helper. Obviously, emitting an HTML table is easy enough, but I'm struggling with returning all the selected rows (or all rows and filtering via a Where(x => x.Selected)).
Here's a dummy version of what I mean:
Model:
public class Player
{
[ScaffoldColumn(false)]
public int Id { get; set; }
public string Name { get; set; }
public int JerseyNumber { get; set; }
public string Position { get; set; }
[ScaffoldColumn(false)]
public bool Selected { get; set; }
}
View:
@model democode.Models.Player
@using (Html.BeginForm())
{
@{
var grid = Html.Grid(Model)
.AutoGenerateColumns()
.Columns(c => c.For(p => Html.CheckBoxFor(_ => p.Selected)).InsertAt(0))
.Columns(c => c.For(p => Html.HiddenFor(_ => p.Id)))
grid.Render();
}
<p>
<input type="submit" value="Submit" />
</p>
}
So, what you are looking at is a grid of hockey players with a checkbox before each one, allowing the user to select one or more. On clicking submit, I'd like for it to post the collection back (understanding that all but Selected and Id would be null/default), but I understand the problem is that the records coming across in the POST data have overlapping keys in the list of key-value pairs.
I have successfully worked around this in the past by hand-writing the HTML table and using the strategy Phil Haack outlines here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
My question is, can I do the same thing using the Grid helper from MvcContrib, or is it more work than what it's worth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论