0

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);
    }
  • You cannot use a `foreach` loop to generate form controls for a collection (refer the dupe to understand how to do it), but the `CheckBox()` method is for binding to a `bool` property, and your `Id` property of `Book` does not sound like it would be a `bool` so that makes no sense anyway. –  Nov 30 '17 at 10:52
  • And you POST method makes even less sense. Your trying to create form controls for a collection of `Book` but then try to bind to a single `Book` in the method. –  Nov 30 '17 at 10:54

0 Answers0