Dot*_*mer 10 c# asp.net asp.net-mvc
我搜索了很多,只花了3天时间搜索和尝试不同的技术(在stackoverflow等),但我找不到在asp.net mvc中实现checkboxlist的解决方案.最后我将我的问题发布到stackoverflow;
所以,我的模型看起来像这样;

我的模型的多对多关系(1个类别可能包含许多项目,一个项目可能属于许多类别)
我的控制器;
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我的看法;
@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>
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是如何在视图中显示类别的复选框列表?
如何从该复选框列表中获取选定的值?
bal*_*dre 17
您需要一个具有所有类别列表的对象,例如,您可以这样做:
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd()
{
// Get all categories and pass it into the View
ViewBag.Categories = db.ListAllCategories();
return View();
}
Run Code Online (Sandbox Code Playgroud)
在视图的顶部
@model Database.Project
@{
// retrieve the list of Categories
List<Database.Category> categories = ViewBag.Categories;
}
Run Code Online (Sandbox Code Playgroud)
然后替换它
<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>
Run Code Online (Sandbox Code Playgroud)
为了这
<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>
Run Code Online (Sandbox Code Playgroud)
回到你的控制器
[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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20987 次 |
| 最近记录: |