每行包含复选框的 Html.Grid

发布于 2024-09-15 17:31:48 字数 338 浏览 9 评论 0原文

我在我的 asp.net MVC2 应用程序中使用 Html.Grid。

网格每行包含复选框。

<%= Html.Grid<MyViewModel>(Model.MyList)
        .Columns( column => {
            column.For(x => Html.CheckBox("Select", false, new { id = x.ID })).DoNotEncode();

我需要循环检查已检查的记录并对其进行操作。当我单击按钮进行回发时,如何检索已检查的 id 值。

谢谢 :)

I am using Html.Grid in my asp.net MVC2 application.

The grid contains checkboxes per row.

<%= Html.Grid<MyViewModel>(Model.MyList)
        .Columns( column => {
            column.For(x => Html.CheckBox("Select", false, new { id = x.ID })).DoNotEncode();

I need to loop through the checked records and action them. How can I retrieve the checked id values when I do a postback on a button click.

Thanks :)

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

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

发布评论

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

评论(3

转身以后 2024-09-22 17:31:49

您可能想查看自定义 ModelBinder。

我正在使用网格,并且有一个如下所示的复选框列:

column.For(x => Html.CheckBox(x.CatalogItemId +  "-rcvd", false)).DoNotEncode().Named("Received");

我在控制器中处理回发的操作签名是这样的:

[HttpPost]
public virtual ActionResult Index([ModelBinder(typeof(PendingRecievedItemsBinder))]IEnumerable<ShelfMyndr.Models.Previews.PendingReceivedCatalogItem> rcvdCatalogItems)

您只需要一个实现 IModelBinder 的类。在 BindModel 方法中,您可以执行如下操作:

foreach (string key in controllerContext.HttpContext.Request.Form.AllKeys.Where(k => controllerContext.HttpContext.Request[k].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Length > 1))
        {
            if (key.EndsWith("-rcvd"))
            {
                ci.IsReceived = true;
            }

使用此方法,我的控制器的操作可以专注于对项目列表执行的操作,而不是迭代 Forms 集合本身。

You might want to look at a custom ModelBinder.

I am using the grid and I have a checkbox column that looks like this:

column.For(x => Html.CheckBox(x.CatalogItemId +  "-rcvd", false)).DoNotEncode().Named("Received");

My action signature in the controller to handle post back is this:

[HttpPost]
public virtual ActionResult Index([ModelBinder(typeof(PendingRecievedItemsBinder))]IEnumerable<ShelfMyndr.Models.Previews.PendingReceivedCatalogItem> rcvdCatalogItems)

You just need a class that implements IModelBinder. In the BindModel method you can do something like this:

foreach (string key in controllerContext.HttpContext.Request.Form.AllKeys.Where(k => controllerContext.HttpContext.Request[k].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Length > 1))
        {
            if (key.EndsWith("-rcvd"))
            {
                ci.IsReceived = true;
            }

Using this, my controller's action can focus on what do do with a list of items, instead of iterating thru the Forms collection itself.

全部不再 2024-09-22 17:31:49

我使用或多或少相同的网格代码来进行此操作。

我定义的复选框列或多或少与您的相同:

column.For(x => Html.SimpleCheckBox("keys", x.Id.ToString())).DoNotEncode();

网格包裹在表单中。

表单回传到以下方法。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Process(IList<int> keys) {
// do stuff with the keys
}

如果您将“Select”作为复选框名称,那么我想参数名称也将是“Select”。

I have this working with more or less the same grid code.

The Checkbox column I have defined more or less the same as yours:

column.For(x => Html.SimpleCheckBox("keys", x.Id.ToString())).DoNotEncode();

The grid is wrapped in a form.

The form posts back to the following method.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Process(IList<int> keys) {
// do stuff with the keys
}

If you have "Select" as the checkbox name, then I guess the parameter name would also be Select.

当爱已成负担 2024-09-22 17:31:49

您可以这样添加,

x.Add().Encoded(false).Sanitized(false).SetWidth(20).RenderValueAs(y => Html.Raw(" 
<input type='checkbox' value='"+y.IsAdmin+"' ) />"));

它会在每一行上添加复选框。

You can add like this

x.Add().Encoded(false).Sanitized(false).SetWidth(20).RenderValueAs(y => Html.Raw(" 
<input type='checkbox' value='"+y.IsAdmin+"' ) />"));

it will add checkbox on each row.

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