我正在使用pubs数据库,基本上我在一个页面上显示作者列表,并且有编辑链接。点击编辑链接后,会显示一个页面,其中包含作者的信息,复选框除了特定作者所写的标题外,还有复选标记。
这是我的代码,直到现在:
HomeController的动作方法:
public ActionResult Edit(string id)
{
author author = db.authors.Include("titleauthors").Where(a => a.au_id == id).Single();
List<TitleViewModel> titleViewModelList = db.titles.Select<title, TitleViewModel>(title =>
new TitleViewModel { IsChecked = false, Title = title.title1, title_id = title.title_id }).ToList();
foreach (var item in author.titleauthors)
{
TitleViewModel titleViewModel = titleViewModelList.Where(model => model.title_id == item.title_id).SingleOrDefault();
if (titleViewModel != null)
titleViewModel.IsChecked = true;
}
AuthorViewModel AuthorViewModel = new AuthorViewModel
{
Author = author,
TitleViewModels = titleViewModelList
};
return View(AuthorViewModel);
}
[HttpPost]
public ActionResult Edit(AuthorViewModel AuthorViewModel)
{
return RedirectToAction("Index");
}
Edit.cshtml:
@model MVCCheckboxList.ViewModels.AuthorViewModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Edit</h2>
<fieldset>
@using (Html.BeginForm())
{
<legend>Edit author</legend>
<table>
<tr>
<td>
@Html.LabelFor(model => model.Author.au_fname)
</td>
<td>
@Html.TextBoxFor(model => model.Author.au_fname)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Author.au_lname)
</td>
<td>
@Html.TextBoxFor(model => model.Author.au_lname)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.Author.address)
</td>
<td>
@Html.TextBoxFor(model => model.Author.address)
</td>
</tr>
<tr>
<td valign="top">
Titles
</td>
<td>
@Html.EditorFor(model => model.TitleViewModels, "TitleViewModels")
</td>
</tr>
<tr>
<td>
<p>
<input type="submit" value="Save" />
</p>
</td>
</tr>
</table>
}
</fieldset>
这是我的AuthorViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCCheckboxList.ViewModels
{
public class AuthorViewModel
{
public List<TitleViewModel> TitleViewModels { get; set; }
public author Author { get; set; }
}
}
这是我的TitleViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCCheckboxList.ViewModels
{
public class TitleViewModel
{
public string title_id { get; set; }
public bool IsChecked { get; set; }
public string Title { get; set; }
}
}
信息在“编辑”页面中正确显示。但是当我点击提交时,AuthorViewModel中的Author属性正确保存了所有数据,但List<TitleViewModel>
中的AuthorViewModel
为空。任何人都可以发现这个错误的原因吗?
这是我的EditorTemplate(TitleViewModels.cshtml
):
@model IEnumerable<MVCCheckboxList.ViewModels.TitleViewModel>
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.HiddenFor(modelItem => item.title_id)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsChecked)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
</tr>
}
</table>
答案 0 :(得分:2)
Here您可以找到有关列表绑定的更多信息。
只需将EditorTemplate的名称更改为TitleViewModel.cshtml
并粘贴该代码:
@model MVCCheckboxList.ViewModels.TitleViewModel
<td>
@Html.HiddenFor(modelItem => Model.title_id)
</td>
<td>
@Html.CheckBoxFor(modelItem => Model.IsChecked)
</td>
<td>
@Html.DisplayFor(modelItem => Model.Title)
</td>
在Edit.cshtml
中使用以下代码为列表中的每个元素生成编辑器
<table>
@for (int i = 0; i < Model.TitleViewModels.Count; i++) {
@Html.EditorFor(m => Model.TitleViewModels[i])
}
</table>