ASP.NET MVC多个复选框

编程入门 行业动态 更新时间:2024-10-28 04:20:07
本文介绍了ASP.NET MVC多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有大约20项的列表我要显示给用户与每个人旁边的复选框(A 可用属性)。

I have a List of about 20 items I want to display to the user with a checkbox beside each one (a Available property on my ViewModel).

当表单提交,我希望能够通过一个通过我的视图模型的选择属性检查回到我的控制器方法每个复选框的值。

When the form is submitted, I want to be able to pass the value of each checkbox that is checked back to my controller method via the Selections property on my ViewModel.

我会如何去这样做使用MVC表单辅助类?这甚至可能?

How would I go about doing this using the Form Helper class in MVC? Is this even possible?

PS:我不希望一个列表框,用户可以只突出多个项目。

PS: I don't want a listbox where the user can just highlight multiple items.

推荐答案

模型:

public class MyViewModel { public int Id { get; set; } public bool Available { get; set; } }

控制器:

public class HomeController: Controller { public ActionResult Index() { var model = Enumerable.Range(1, 20).Select(x => new MyViewModel { Id = x }); return View(model); } [HttpPost] public ActionResult Index(IEnumerable<MyViewModel> model) { ... } }

查看〜/查看/主页/ Index.cshtml :

@model IEnumerable<AppName.Models.MyViewModel> @using (Html.BeginForm()) { @Html.EditorForModel() <input type="submit" value="OK" /> }

编辑模板〜/查看/主页/ EditorTemplates / MyViewModel.cshtml :

@model AppName.Models.MyViewModel @Html.HiddenFor(x => x.Id) @Html.CheckBoxFor(x => x.Available)

更多推荐

ASP.NET MVC多个复选框

本文发布于:2023-10-12 01:40:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   复选框   ASP   NET   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!