我在我的控制器中创建了create function,它显示了这样的checkox中的所有类型:
控制器:
public ActionResult Create()
{
var db = new MainDatabaseEntities();
var viewModel = new ANIME
{
GENRES = db.GENRES.Select(c => new { ID_GE = c.ID_GE, GENRE = c.GENRE, isSelected = false }).ToList().Select(g => new GENRES
{
ID_GE = g.ID_GE,
GENRE = g.GENRE,
isSelected = false
}).ToList()
};
return View(viewModel);
}
查看:
@model AnimeWeb.Models.ANIME
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
<tr>
<td class="lefttablecontent" id="top">
<div class="editor-label">
@Html.HiddenFor(model => model.ID_AN)
</div>
<div class="editor-label">
Original title
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TITLE_OR)
@Html.ValidationMessageFor(model => model.TITLE_OR)
</div>
<div class="editor-label">
English title
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TITLE_EN)
@Html.ValidationMessageFor(model => model.TITLE_EN)
</div>
<div class="editor-label">
Genres
</div>
<div class="editor-list">
@Html.EditorFor(model => model.GENRES)
@Html.ValidationMessageFor(model => model.GENRES)
</div>
</td>
<td class="righttablecontent" id="top">
<p>
<input type="submit" value="Create" />
</p>
</td>
</tr>
</table>
}
}
类型的EditorTemplate:
@model AnimeWeb.Models.GENRES
@Html.HiddenFor(model => model.ID_GE)
@Html.HiddenFor(model => model.GENRE)
<table>
<tr>
<td class=" right">
@Html.EditorFor(model => model.isSelected)
</td>
<td>
@Html.LabelFor(model => model.isSelected, Model.GENRE)
</td>
</tr>
</table>
现在我想显示所有类型的相同模板,并将其用于索引中的过滤,但我不知道该怎么做。
控制器:
public ActionResult Index(string sortOrder, string currentFilter, int? page)
{
using (var db = new MainDatabaseEntities())
{
ViewBag.CurrentSort = sortOrder;
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var anime = from s in db.ANIME.Include("GENRES")
select s;
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(anime.ToPagedList(pageNumber, pageSize));
}
}
查看:
@model PagedList.IPagedList<AnimeWeb.Models.ANIME>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Index";
}
<p>
@if (Roles.IsUserInRole("Administrator"))
{
@Html.ActionLink("Create New", "Create")
}
</p>
@foreach (var item in Model) //
{ //
@Html.EditorFor(modelItem => item.GENRES); // I'm not sure about this line
} //
<table>
<tr>
<th>
Anime
</th>
<th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink(item.TITLE_OR, "Details", new { id = item.ID_AN })
</td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
当我尝试使用@ Html.EditorFor(modelItem =&gt; item.GENRES)显示它时,它只显示在每个动画中选择的类型,但我需要所有类型来过滤它们。我甚至不确定这是一个很好的方法。有什么建议吗?