C# Asp.Net MVC4 显示复选框列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12650441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp.Net MVC4 Display CheckboxList
提问by Idrees Khan
I have searched a lot and spend 3 days only for searching and trying different technique (on stackoverflow etc) but I find no solution for implementing checkboxlist in asp.net mvc. And at last I am posting my problem to stackoverflow;
So, my model looks like this;
我搜索了很多东西,只花了 3 天时间来搜索和尝试不同的技术(在 stackoverflow 等上),但我没有找到在 asp.net mvc 中实现复选框列表的解决方案。最后我将我的问题发布到 stackoverflow;
所以,我的模型看起来像这样;


Many to Many relationship of my model(1 category may contain many projects and a project may belongs to many categories)
My controller;
我的模型的多对多关系(1个类别可能包含多个项目,一个项目可能属于多个类别)
我的控制器;
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd()
{
return View();
}
My view;
我的看法;
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Add New Project</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectHeading)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjectHeading)
@Html.ValidationMessageFor(model => model.ProjectHeading)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProjecctUrl)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjecctUrl)
@Html.ValidationMessageFor(model => model.ProjecctUrl)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectLongDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjectLongDescription)
@Html.ValidationMessageFor(model => model.ProjectLongDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PromoFront)
</div>
@Html.EditorFor(model => model.PromoFront)
@Html.ValidationMessageFor(model => model.PromoFront)
<div class="editor-label">
@Html.LabelFor(model => model.ProjectThubmnail)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjectThubmnail)
@Html.ValidationMessageFor(model => model.ProjectThubmnail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectImage)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjectImage)
@Html.ValidationMessageFor(model => model.ProjectImage)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CategoryId)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
<p>
<input type="submit" value="Create" class="submit" />
</p>
So, my question is How do I display checkboxlist for categories in my view?
How do I get selected values from that checkboxlist?
所以,我的问题是如何在我的视图中显示类别的复选框列表?
如何从该复选框列表中获取选定的值?
采纳答案by balexandre
You need to have an object that will have a list of all categories, for example, you could do this:
您需要有一个包含所有类别列表的对象,例如,您可以这样做:
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd()
{
// Get all categories and pass it into the View
ViewBag.Categories = db.ListAllCategories();
return View();
}
in the top of your View
在您的视图顶部
@model Database.Project
@{
// retrieve the list of Categories
List<Database.Category> categories = ViewBag.Categories;
}
and then replace this
然后替换这个
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CategoryId)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
for this
为了这
<div class="editor-label">
<label for="categories">Categories</label>
</div>
<div class="editor-field">
@foreach(var c in categories) {
<label class="checkbox">
<input type="checkbox" name="categories" value="@c.CategoryId"> @c.CategoryName
</label>
}
</div>
back in your Controller
回到你的控制器
[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd(Database.Project model, int[] categories)
{
if(ModelState.IsValid) {
// fill up categories
db.InsertAndSaveProject(model, categories);
}
...
return redirectToView("ProjectAdd");
}
回答by Rohit
Answer suggested by @balexandre above works great. However, I used following HTML Extension for CheckBoxList.
上面@balexandre 建议的答案效果很好。但是,我为 CheckBoxList 使用了以下 HTML 扩展。
1. CheckboxList in ASP.net MVC4
2. Source Code:HTML Extension Helper method (Github)
1. ASP.net MVC4 中的 CheckboxList
2. 源代码:HTML 扩展助手方法 (Github)
The Major advantage of using this helper method is clean code and better readability. E.g.
使用这个辅助方法的主要优点是干净的代码和更好的可读性。例如
@Html.CheckBoxListFor(model => model.SelectedItems, Model.AllItems)

