MVC控制器获得“复选框列表”的值

编程入门 行业动态 更新时间:2024-10-26 16:29:48
本文介绍了MVC控制器获得“复选框列表”的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在这里可能是个白痴,但在获取是否已选中/未选中复选框的值方面遇到问题。这是到目前为止我得到的:

I'm probably a idiot here but I'm having problems getting the value of whether or not a checkbox is checked/selected or not. Here's what I've got so far:

在我的模型中:

public IEnumerable<SelectListItem> Insurers { get { var list = new List<SelectListItem>(); string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton"; string[] zInsurers = zInsurersList.Split(','); foreach (string aInsurer in zInsurers) { list.Add(new SelectListItem() { Text = aInsurer, Value = aInsurer, Selected=false}); } return list; } } }

我的观点:

@foreach (var insurer in @Model.Insurers) { var zInsurer = insurer.Text; var zValue = insurer.Value; <tr> <td style="width: 120px; height: 35px;"><span id="@zInsurer">@zInsurer</span></td> <td style="width: 40px; height: 35px;"><input id="@zInsurer" type="checkbox" name="@zInsurer"></td> </tr> }

所以在我的控制器中,我试图循环列表并获取值用户是否选择了以下选项:

So in my controller I'm trying to loop the list and get the value of whether or not the user has selected the option:

foreach (var item in model.Insurers) { //if (item.GetType() == typeof(CheckBox)) //string controlVal = ((SelectListItem)item).Selected.ToString(); zInsurers = zInsurers + item.Text + " " + ((SelectListItem)item).Selected.ToString() + "<br/>"; }

但该值始终返回false。

But the value always returns false.

有人可以花点时间强调我的愚蠢吗?

Could someone spare a few mins to highlight my stupidity please?

谢谢

Craig

推荐答案

有很多方法可以做到这一点。我通常在模型中添加 String Array 来收集选定的值。

There are a lot of ways to do it. I normally add String Array in model to collect selected values.

public string[] SelectedInsurers { get; set; } <input type="checkbox" name="SelectedInsurers" value="@insurer.Value" />

这是示例代码-

public class MyModel { public string[] SelectedInsurers { get; set; } public IEnumerable<SelectListItem> Insurers { get { var list = new List<SelectListItem>(); string zInsurersList = "Age UK,Be Wiser,Call Connection,Churchill,Sainsbury's,Direct Line,Hastings Direct,LV=,Nationwide,RIAS,Swinton"; string[] zInsurers = zInsurersList.Split(','); foreach (string aInsurer in zInsurers) { list.Add(new SelectListItem { Text = aInsurer, Value = aInsurer, Selected = false }); } return list; } } }

操作方法

Action Methods

public ActionResult Index() { return View(new MyModel()); } [HttpPost] public ActionResult Index(MyModel model) { return View(); }

查看

View

@using (Html.BeginForm()) { foreach (var insurer in @Model.Insurers) { <input type="checkbox" name="SelectedInsurers" value="@insurer.Value" /> @insurer.Text<br/> } <input type="submit" value="Post Back" /> }

结果

Result

更多推荐

MVC控制器获得“复选框列表”的值

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

发布评论

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

>www.elefans.com

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