0
我正在使用pubs数据库,基本上我在一个页面上显示了作者列表,并且有编辑链接。在点击编辑链接时,除了特定作者所写的标题外,还会显示一个页面,其中包含作者信息和带复选标记的复选框列表。数据未采取行动方法绑定
这是我的代码至今:
的HomeController action方法:
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>
为空。任何人都可以发现错误,为什么会发生这种情况?
这是我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>
我想你说的话但这不起作用。我收到异常“Visual Studio 2010 \ Projects \ MVCCheckboxList \ MVCCheckboxList \ Views \ Shared \ EditorTemplates \ TitleViewModel.cshtml(3):error CS0103:名称'item'在当前上下文中不存在' – Jaggu
好的对不起!有用。你问我粘贴代码,所以我粘贴没有使用常识。 @ Html.HiddenFor(modelItem => item.title_id)必须是@ Html.HiddenFor(modelItem => modelItem .title_id);)。谢谢krolik – Jaggu
对不起,我编辑了我的答案(使用'Model'或'modelItem'代替'item') – krolik