CodeGo.net>如何将复杂的对象集合传递给控制器​​在MVC

我是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>
    }

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

解决方法:

为了将集合发布回去,您需要按照以下方式对它们进行索引,以使模型活页夹可以将它们拾取.

这应该可以解决问题:

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

上一篇:C#-MVC 4视图重新加载数据更改


下一篇:使用c#将文件夹从一个驱动器移动到另一个驱动器