MVC4 - 提交到主视图期间的部分视图列表模型绑定
MVC4 - Partial View List Model binding during Submit to Main view
我有一个视图模型,其中有一个子模型列表来呈现部分视图(如下)。
public class PRDocument
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[DisplayName("Vendor Name")]
[Column(Order = 2)]
public int VendorId { get; set; }
public virtual ICollection<PRDocumentQuotation> PRDocumentQuotations { get; set; }
[NotMapped]
public List<PRDocumentQuotation> Quotations { get; set; }
public PRDocument()
{
Quotations = new List<PRDocumentQuotation>();
}
}
public class PRDocumentQuotation
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[Required]
[Column(Order = 1)]
public int PRDocumentId { get; set; }
[Display(Name = "Uploaded File")]
[Column(Order = 2)]
public string FileName { get; set; }
}
并且在这样渲染的局部视图中。
@Html.Partial("_PRDocs", Model.Quotations)
这是我的部分观点。
@model IEnumerable<JKLLPOApprovalApp.Models.PRDocumentQuotation>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FileName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
@Html.HiddenFor(modelItem => item.PRDocumentId)
<td>
@Html.DisplayFor(modelItem => item.FileName)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
控制器动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PRDocument pRDocument)
{
if (ModelState.IsValid)
{
pRDocument.PRDocumentQuotations = pRDocument.Quotations;
db.tbl_PRDocuments.Add(pRDocument);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pRDocument);
}
查看数据主视图
@model JKLLPOApprovalApp.Models.PRDocument
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PRDocument</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.VendorId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VendorId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.VendorId, "", new { @class = "text-danger" })
</div>
</div>
@Html.Partial("_PRDocs", Model.Quotations)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我想要的是将部分视图数据列表 (PRDocumentQuotation) 获取到创建操作中,与主模型 (PRDocument) 绑定。我该怎么做?
有趣的问题:)
问题是 Model
绑定到列表应该有唯一的名称。所以生成的 HTML
应该如下所示:
<input id="Quotations_0__PRDocumentId" name="Quotations[0].PRDocumentId" type="hidden" value="0">
<input id="Quotations_1__PRDocumentId" name="Quotations[1].PRDocumentId" type="hidden" value="0">
推荐的解决方案是使用编辑器模板,勾选this and 。
但我在下面给出了替代解决方案,使用 for 循环创建带索引的唯一名称,取自面临同样问题的 this post。
在主视图中:
改为传递主模型
@Html.Partial("_PRDocs", Model)
局部视图:
@model JKLLPOApprovalApp.Models.PRDocument
<table class="table">
@if (Model.Quotations != null)
{
for (var i = 0; i < Model.Quotations.Count(); i++)
{
<tr>
<th>
@Html.DisplayNameFor(model => Model.Quotations[i].FileName)
</th>
<th></th>
</tr>
<tr>
@Html.HiddenFor(modelItem => Model.Quotations[i].PRDocumentId)
<td>
@Html.DisplayFor(modelItem => Model.Quotations[i].FileName)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = Model.Quotations[i].Id })
</td>
</tr>
}
}
</table>
希望有所帮助。
我有一个视图模型,其中有一个子模型列表来呈现部分视图(如下)。
public class PRDocument
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[DisplayName("Vendor Name")]
[Column(Order = 2)]
public int VendorId { get; set; }
public virtual ICollection<PRDocumentQuotation> PRDocumentQuotations { get; set; }
[NotMapped]
public List<PRDocumentQuotation> Quotations { get; set; }
public PRDocument()
{
Quotations = new List<PRDocumentQuotation>();
}
}
public class PRDocumentQuotation
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[Required]
[Column(Order = 1)]
public int PRDocumentId { get; set; }
[Display(Name = "Uploaded File")]
[Column(Order = 2)]
public string FileName { get; set; }
}
并且在这样渲染的局部视图中。
@Html.Partial("_PRDocs", Model.Quotations)
这是我的部分观点。
@model IEnumerable<JKLLPOApprovalApp.Models.PRDocumentQuotation>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FileName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
@Html.HiddenFor(modelItem => item.PRDocumentId)
<td>
@Html.DisplayFor(modelItem => item.FileName)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
控制器动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PRDocument pRDocument)
{
if (ModelState.IsValid)
{
pRDocument.PRDocumentQuotations = pRDocument.Quotations;
db.tbl_PRDocuments.Add(pRDocument);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pRDocument);
}
查看数据主视图
@model JKLLPOApprovalApp.Models.PRDocument
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PRDocument</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.VendorId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VendorId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.VendorId, "", new { @class = "text-danger" })
</div>
</div>
@Html.Partial("_PRDocs", Model.Quotations)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我想要的是将部分视图数据列表 (PRDocumentQuotation) 获取到创建操作中,与主模型 (PRDocument) 绑定。我该怎么做?
有趣的问题:)
问题是 Model
绑定到列表应该有唯一的名称。所以生成的 HTML
应该如下所示:
<input id="Quotations_0__PRDocumentId" name="Quotations[0].PRDocumentId" type="hidden" value="0">
<input id="Quotations_1__PRDocumentId" name="Quotations[1].PRDocumentId" type="hidden" value="0">
推荐的解决方案是使用编辑器模板,勾选this and
但我在下面给出了替代解决方案,使用 for 循环创建带索引的唯一名称,取自面临同样问题的 this post。
在主视图中:
改为传递主模型
@Html.Partial("_PRDocs", Model)
局部视图:
@model JKLLPOApprovalApp.Models.PRDocument
<table class="table">
@if (Model.Quotations != null)
{
for (var i = 0; i < Model.Quotations.Count(); i++)
{
<tr>
<th>
@Html.DisplayNameFor(model => Model.Quotations[i].FileName)
</th>
<th></th>
</tr>
<tr>
@Html.HiddenFor(modelItem => Model.Quotations[i].PRDocumentId)
<td>
@Html.DisplayFor(modelItem => Model.Quotations[i].FileName)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = Model.Quotations[i].Id })
</td>
</tr>
}
}
</table>
希望有所帮助。