MVC4当父视图单击提交时如何在部分视图中获取数据

时间:2014-07-17 04:50:03

标签: model-view-controller view parent partial

MVC4当父视图单击提交时如何在局部视图中获取数据

我创建了一个用于更新数据的编辑页面,父视图显示了一些书籍细节,而局部视图则是一个循环显示每本书的状态。此外,父视图有一个提交按钮,用于更新部分视图和父视图,但是当我单击提交时,仅父视图数据可以更新,部分视图仍然不更新。以下代码:

型号:

public class LibraryInventory
{
      public decimal LibraryID { get; set; }
      public string Title { get; set; }
      public List<LibraryItem> Entities { get; set; }
}

public class LibraryItem
{     public decimal StatusID { get; set; }
      public string Location { get; set; }
      public decimal BorrowedBy { get; set; }
}

控制器:

public ActionResult EditRecord(string ID)
{    
     DataTable dt = (DataTable)Session["EditGridData"];
     LibraryInventory record = new LibraryInventory(dt.Rows[0]);
     dt = LibraryEditBLL.GetEditItems(ID);
     if (results.ToList().Count > 0)
     {
          record.Entities = LibraryItem.ConvertToLibraryEntity(dt).ToList();
     }
     return View(record);
}


[HttpPost]
public ActionResult EditRecord(string FormButton, LibraryInventory model)
{   
     switch (FormButton)
     {    
       case "Submit":                    
           if (ModelState.IsValid)
           {       
               LibraryEditBLL.UpdateInventoryLibrary(model);
           }
           return View("EditRecord",record);
      default:
           return RedirectToAction("Edit"); //other page not need check
 }

查看:

 @model XXX.Models.LibraryModels.LibraryInventory                             
 @using (Html.BeginForm("EditRecord", "Library", FormMethod.Post, new { enctype = "multipart/form-data" })){
 @Html.HiddenFor(m =>m.LibraryID)
 <table>
     <tr>
        <td>@Html.TextBoxFor(m => m.Title)</td> 
        @for (var i = 0; i < @Model.Entities.Count; i++)
        {
            @Html.Partial("EditItem", @Model.Entities[i])

        }
     </tr>
 </table>
  }




  @model XXX.Models.LibraryModels.LibraryItem
  <tr>
      <td> @Html.DropDownListFor(m => m.StatusID) </td>
      <td> @Html.DropDownListFor(m => m.Location)</td>
      <td> @Html.DropDownListFor(m => m.BorrowedBy) </td>
  </tr>

1 个答案:

答案 0 :(得分:0)

尝试使用此代码进行查看。对于这个简单的场景,无需局部视图。

@model XXX.Models.LibraryModels.LibraryInventory                             
@using (Html.BeginForm("EditRecord", "Library", FormMethod.Post, new { enctype = "multipart/form-data" })){
@Html.HiddenFor(m =>m.LibraryID)
<table>
 <tr>
    <td>@Html.TextBoxFor(m => m.Title)</td> 
 </tr>
    @for (var i = 0; i < Model.Entities.Count; i++)
    {          
     <tr>
         <td> @Html.DropDownListFor(m => Model.Entities[i].StatusID) </td>
          <td> @Html.DropDownListFor(m => Model.Entities[i].Location)</td>
            <td> @Html.DropDownListFor(m => Model.Entities[i].BorrowedBy) </td>

     </tr>
    }

</table>
 }
相关问题