ASP.Net MVC 3获取复选框列表值

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

我正在开发一个ASP.Net MVC 3 Web应用程序,我有从的CheckBoxList得到的值一定的困难。我已阅读大部分的#2的问题在这附近,但是,我仍然有一些问题。

I am developing an ASP.Net MVC 3 Web application and I am having some difficulties with getting the values from a checkboxlist. I have already read most of the questions on Stackoverflow around this area, however, I am still having some issues.

我有一个视图模型

public class ViewModelCheckBox { public string Id { get; set; } public string Name { get; set; } public bool Checked { get; set; } }

另一个视图模型而用上面视图模型

public class ViewModelAssignSubSpeciality { public ListItem Item { get; set; } public IList<ViewModelCheckBox> SpecialityList { get; set; } }

然后在我的控制器

Then in my controller

public ActionResult AssignSubSpeciality(int id) { //Get a list of all sub-specialities var SpecialityList = _listService.GetListItemsByID(3).ToList(); //Get a list of sub-specialities for the the passed in id, this is either the id of a speciality or grade IList<RelationshipSpecialitySub> assignedSpecialities = _listService.GetAssignedSubSpecialities(id).ToList(); var checkBoxList = new List<ViewModelCheckBox>(); foreach (ListItem item in SpecialityList) { ViewModelCheckBox chkBox = new ViewModelCheckBox { Id = item.listItemID.ToString(), Name = item.description }; //If sub-speciality exists in assignedSpecialities list, then make checkbox checked foreach (var specilaity in assignedSpecialities) { if (specilaity.subID == item.listItemID) { chkBox.Checked = true; } else { chkBox.Checked = false; } } checkBoxList.Add(chkBox); } ViewModelAssignSubSpeciality viewModel = new ViewModelAssignSubSpeciality(); viewModel.ListItem = _listService.GetListItemByID(id); viewModel.SpecialityList = checkBoxList; return View(viewModel); }

在code在上面的控制器让所有的可能的复选框列表项的列表,然后让所有pviously选中的复选框列表项$ P $为它设置检查选项设置为true的列表。

The code in the controller above is getting a list of all the possible checkbox list items, then getting a list of all the previously selected checkbox list items for which it sets the checked option to true.

我的视图则看起来像这样,循环在SpecialityList,并对每个项目创建一个复选框,而且如果需要的话其选定的值设置为true。

My View then looks like this, looping over the SpecialityList and creating a checkbox for each item, and also setting its selected value to true if needs be.

<fieldset> <legend>Specialities</legend> @foreach (var item in Model.SpecialityList) { <input type="checkbox" id="@item.Id" name="@item.Name" value="@item.Id" @(item.Checked ? "checked" : "") /> <label for="@item.Id">@item.Name</label><br /> } <input type="submit" value="Save Changes" class="sepH_b" />

我HttpPost在我的控制器方法,那么看起来像这样

My HttpPost method in my controller then looks like this

public ActionResult AssignSubSpeciality(ViewModelAssignSubSpeciality model) { //delete all sub-specialities in tbl relationshipSpecialitySub for List foreach (ViewModelCheckBox item in model.SpecialityList) { //_listService.DeleteSubSpecialityFromSpeciality(item.Id); } return RedirectToAction("ListItems", new { id = model.ListItem.listID }); }

然而,当我试图让在选定的复选框

However, when I try to get the selected checkboxes in

model.SpecialityList

据我们永远空。我不知道为什么它不包含ViewModelCheckBox的列表。

It us always null. I am not sure why it doesnt contain a list of ViewModelCheckBox.

任何人都可以请帮我这个?

Can anyone please help me with this?

感谢您。

推荐答案

我有一个这些在可枚举我视图模型

I have an Enumerable of these in my view model

public class CheckBoxItem { public string Code { get; set; } public bool IsChecked { get; set; } public string Label {get;set;} }

然后我使用编辑器模板在页面上显示出来。

I then use an editor template to display them on the page.

<p class="checkbox" style="display:inline"> <span style="margin-left:5px;"> @Html.HiddenFor(x => x.Code) @Html.CheckBoxFor(x => x.IsChecked) </span> @Html.LabelFor(x => x.IsChecked, Model.Label) </p>

在我用下面的页面上显示它们的看法。

In the view I use the following to display them on the page.

@Html.EditorFor(m => m.MyEnumerableOfCheckBoxItem)

当表单被调回该模型是正确的约束。

When the form is posted back the model is correctly bound.

希望这有助于。

更多推荐

ASP.Net MVC 3获取复选框列表值

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

发布评论

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

>www.elefans.com

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