Asp.Net MVC4显示的CheckBoxList [英] Asp.Net MVC4 Display CheckboxList
问题描述
我寻觅了很多,花3天仅用于搜索和尝试不同的技术(在计算器等),但我发现在asp.net的MVC实施的CheckBoxList无解。而在最后我张贴我的问题计算器;
所以,我的模型看起来像这样;
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;
很多到我的模型的许多关系(1类可能包含很多项目,一个项目可能属于多个类别)
我的控制器;
Many to Many relationship of my model(1 category may contain many projects and a project may belongs to many categories)
My controller;
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd()
{
return 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>
所以,我的问题是如何我在我看来显示类别的CheckBoxList吗
我如何从的CheckBoxList选择的值?
推荐答案
您需要有一个对象,将所有类别的列表,例如,你可以这样做:
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();
}
在您的视图的顶部
@model Database.Project
@{
// retrieve the list of Categories
List<Database.Category> categories = ViewBag.Categories;
}
,然后替换该
<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>
此
<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>
回到你的控制器
[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");
}
这篇关于Asp.Net MVC4显示的CheckBoxList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!