MVC4如何在父视图单击提交时获取部分视图中的数据
Posted
技术标签:
【中文标题】MVC4如何在父视图单击提交时获取部分视图中的数据【英文标题】:MVC4 How to get the data in partial view when parent view click submit 【发布时间】:2014-07-17 04:50:02 【问题描述】: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】:试试这个代码以获得你的观点。对于这个简单的场景,不需要局部视图。
@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>
【讨论】:
以上是关于MVC4如何在父视图单击提交时获取部分视图中的数据的主要内容,如果未能解决你的问题,请参考以下文章