I am using pubs database and basically I show a list of authors on one page and there is edit link. On click of edit link, one page gets showed which has author’s information and checkboxlist with checkmark besides the titles that the particular author have written.
This is my code till now:
Action methods of 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>
This is my 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; }
}
}
and this is my 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; }
}
}
The information gets properly displayed in Edit page. But when I click on submit, the Author property in AuthorViewModel correctly holds all the data but the List<TitleViewModel> in AuthorViewModel is null. Can anybody spot the mistake why is this happening?
This is my 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>
Here you find more informations about list binding.
Just change the name of your EditorTemplate to
TitleViewModel.cshtmland paste that code:In
Edit.cshtmluse below code to generate editor for each element from your list