当我的视图模型包含来自2个不同模型的数据时,如何将数据从视图返回到我的编辑控制器?

时间:2017-11-30 10:47:36

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我无法弄清楚如何将数据从我的视图返回给我的控制器。我的视图有一个包含2个模型的视图模型。这个视图模型我命名为Model Holder。我能够从Model Holder类操作我的视图上的数据而没有任何问题。但是当我按下保存按钮时,我会继续收到错误。下面是我的ModelHolder类,My View和我的Controller的代码。任何帮助将非常感激。

我的ModelHolder类

    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>();
        }

  }

我的观点

    @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>

我的控制器方法

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);
    }

0 个答案:

没有答案