如何在mvc中将复杂对象集合传递给控制器

时间:2013-12-15 20:37:47

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

我是MVC的新手,对这个初学者问题感到抱歉。我有以下模型类:

public class ReturnBookHedModel
{

    public int RefferenceID { get; set; }
    public int BorrowedRefNo { get; set; }
    public int MemberId { get; set; }
    public DateTime ReturnDate { get; set; }
    public bool IsNeedToPayFine { get; set; }       
    public DateTime CurrentDate { get; set; }

    public virtual List<ReturnBookDetModel> RetunBooks { get; set; }
    public virtual MemberModel member { get; set; }
}

public class ReturnBookDetModel
{

    public int BookID { get; set; }
    public int RefferenceID { get; set; }
    public bool IsReturned { get; set; }
    public virtual ReturnBookHedModel ReturnBookHed { get; set; }
    public virtual BookModel book { get; set; }
}

我有以下控制器方法:

    public ActionResult SaveReturnBook(int refNo)
    {
        ReturnBookHedModel model = ReturnBookFacade.GetReturnBookBasedOnRefference(refNo);
        return View(model);
    }

    //
    // POST: /ReturnBook/Create

    [HttpPost]
    public ActionResult SaveReturnBook(ReturnBookHedModel model)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

在我的模型中我定义如下:

     <div class="control-label">
          @Html.LabelFor(model => model.BorrowedRefNo)
          @Html.TextBoxFor(model => model.BorrowedRefNo, new { @class = "form-control" ,@readonly = "readonly" })
          @Html.ValidationMessageFor(model => model.BorrowedRefNo)

     </div>
      // rest of the header details are here
    <table>
        <tr>                                   
            <th>
                @Html.DisplayNameFor(model => model.RetunBooks.FirstOrDefault().IsReturned)
             </th>
             <th>
                 @Html.DisplayNameFor(model => model.RetunBooks.FirstOrDefault().BookID)
             </th>
              <th>
                 @Html.DisplayNameFor(model => model.RetunBooks.FirstOrDefault().book.BookName)
              </th>
              <th></th>
            </tr>

            @foreach (var item in Model.RetunBooks)
             {
                <tr >                                        
                   <td>
                       @Html.CheckBoxFor(modelItem => item.IsReturned)
                   </td>
                   <td>
                       @Html.HiddenFor(modelItem => item.BookID);
                       @Html.DisplayFor(modelItem => item.BookID)
                    </td>
                    <td>
                       @Html.DisplayFor(modelItem => item.book.BookName)
                     </td>
                  </tr>
                }
</table>

这工作得很好..但是这些表格细节(复杂对象)不在控制器的post方法中。当我搜索我发现我可以使用这个详细数据如下:但我不能使用它如下。

  @for (var i = 0; i < Model.RetunBooks.Count; i++) 
   {
      <tr>                                        
          <td>
              @Html.CheckBoxFor(x => x.RetunBooks.)
           </td>                                        
       </tr>
    }

如何将这些信息发送给控制器

1 个答案:

答案 0 :(得分:4)

为了回收集合,您需要按照以下方式对其进行索引,以便模型绑定器选择它们。

这应该可以解决问题:

 @for (var i = 0; i < Model.RetunBooks.Count; i++) 
 {
      ...
      @Html.CheckBoxFor(model => Model.RetunBooks[i].IsReturned)
      ...
 }

复杂对象需要以上述方式进行索引。

有关它的更多信息,请参见此处:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/