I cant figure out how to return the data from my view back to my controller. My view has a view model which contains 2 models. This view model I named Model Holder. I am able to manipulate the data on my view from the Model Holder class with no issues. But When I press the save button I keep on getting errors. Below is code from my ModelHolder class, My View, and my Controller. Any help would be much appreciated.
My ModelHolder Class
public class ModelHolder
{
private LibraryDBEntities db = new LibraryDBEntities();
public List<Author> authors;
public List<Book> books;
int? id;
public ModelHolder(int? id)
{
this.id = id;
authors = new List<Author>();
books = new List<Book>();
}
}
My View
@model TM_TWA.ModelHolder
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Author</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.authors.FirstOrDefault().Id)
<div class="form-group">
@Html.LabelFor(model => model.authors.FirstOrDefault().FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.authors.FirstOrDefault().FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.authors.FirstOrDefault().FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.authors.FirstOrDefault().LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.authors.FirstOrDefault().LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.authors.FirstOrDefault().LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.authors.FirstOrDefault().Website, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.authors.FirstOrDefault().Website, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.authors.FirstOrDefault().Website, "", new { @class = "text-danger" })
</div>
</div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.books.First().Title)
</th>
<th>
@Html.DisplayNameFor(model => model.books.First().PublicationDate)
</th>
<th></th>
</tr>
@foreach (var n in Model.books)
{
<tr>
<td>
@Html.CheckBox(n.Id.ToString(), n.Id.ToString())
@Html.DisplayFor(modelItem => n.Title)
</td>
<td>
@Html.DisplayFor(modelItem => n.PublicationDate)
</td>
</tr>
}
</table>
<!--
-->
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
My Controller Method
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,Website")] Author author, [Bind(Include = "Id")] Book book)
{
if (ModelState.IsValid)
{
db.Entry(book).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(book);
}