我有一个包含以下表格的数据库:DIM_Invoice,DIM_Carton,DIM_InvoiceItem。这是一个分层结构 - 发票包含一个或多个纸箱。纸箱包含一个或多个InvoiceItems。
最终我需要创建一个分层视图,它将显示发票信息,与发票相关的纸箱以及与每个纸箱相关的InvoiceItems。每个级别都有一些应该可编辑的字段。
但是现在,我只是想让前两个级别工作:一个发票有多个与之相关的纸箱。
以下是我的模型,它们是由实体框架生成的:
namespace Invoices2.Models
{
using System;
using System.Collections.Generic;
public partial class DIM_Invoice
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DIM_Invoice()
{
this.DIM_Carton = new HashSet<DIM_Carton>();
}
public int LPK_Invoice { get; set; }
public string InvoiceNumber { get; set; }
public string PONumber { get; set; }
public Nullable<System.DateTime> InvoiceDate { get; set; }
public Nullable<System.DateTime> OrderDate { get; set; }
public Nullable<System.DateTime> ShipDate { get; set; }
public string Distributor { get; set; }
public Nullable<bool> AllReceived { get; set; }
public Nullable<int> RowVersion { get; set; }
public string Comments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DIM_Carton> DIM_Carton { get; set; }
}
}
namespace Invoices2.Models
{
using System;
using System.Collections.Generic;
public partial class DIM_Carton
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DIM_Carton()
{
this.DIM_InvoiceItem = new HashSet<DIM_InvoiceItem>();
}
public int LPK_Carton { get; set; }
public string CartonNumber { get; set; }
public Nullable<int> LPK_Invoice { get; set; }
public string InvoiceNumber { get; set; }
public Nullable<int> RowVersion { get; set; }
public string Comments { get; set; }
public virtual DIM_Invoice DIM_Invoice { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<DIM_InvoiceItem> DIM_InvoiceItem { get; set; }
}
}
以下是DIM_InvoiceController的编辑部分。同样,它们是由Visual Studio自动生成的,除了我编辑了Post部分,因此它不会重定向到Index视图。
// GET: DIM_Invoice/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
DIM_Invoice dIM_Invoice = db.DIM_Invoice.Find(id);
if (dIM_Invoice == null)
{
return HttpNotFound();
}
return View(dIM_Invoice);
}
// POST: DIM_Invoice/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "LPK_Invoice,InvoiceNumber,PONumber,InvoiceDate,OrderDate,ShipDate,Distributor,AllReceived,RowVersion,Comments")] DIM_Invoice dIM_Invoice)
{
if (ModelState.IsValid)
{
db.Entry(dIM_Invoice).State = EntityState.Modified;
db.SaveChanges();
return View(dIM_Invoice);
//return RedirectToAction("Index");
}
return View(dIM_Invoice);
}
我已将自动生成的DIM_Carton编辑视图的副本移动到Shared / EditorTemplates文件夹中,以便它可以充当EditorTemplate,然后将其简化并删除一些字段,因为我只需要显示CartonNumber字段:
@model Invoices2.Models.DIM_Carton
<div class="form-horizontal">
<h4>DIM_Carton</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.LPK_Carton)
<div class="form-group">
@Html.LabelFor(model => model.CartonNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CartonNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CartonNumber, "", new { @class = "text-danger" })
</div>
</div>
</div>
最后,这是DIM_Invoice的编辑视图,它试图通过调用@ Html.EditorFor(model =&gt; model.DIM_Carton)来利用DIM_Carton EditorTemplate
@model Invoices2.Models.DIM_Invoice
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DIM_Invoice</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.LPK_Invoice)
<div class="form-group">
@Html.LabelFor(model => model.InvoiceNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InvoiceNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InvoiceNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PONumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PONumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PONumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InvoiceDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InvoiceDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InvoiceDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OrderDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShipDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShipDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ShipDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Distributor, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Distributor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Distributor, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AllReceived, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.AllReceived)
@Html.ValidationMessageFor(model => model.AllReceived, "", new { @class = "text-danger" })
</div>
</div>
</div>
// ***Here's the call to the EditorTemplate***
<div class="form-group">
<div class="col-md-10">
@Html.EditorFor(model => model.DIM_Carton)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
好的,我是MVC的新手,请耐心等待......这是我要解决的具体问题:
首先,我尝试使用局部视图,其中DIM_Invoice Edit视图将DIM_Carton Edit视图视为局部视图。但是,每当在浏览器中对DIM_Invoice进行编辑并回发时,部分视图中显示的信息将不会被保留(即,由于缺少更好的描述,它只是“消失”)。
在Google上搜索后,我找到了post here on stackoverflow,其中说我应该使用EditorTemplates。该帖子提供了resource for EditorTemplates,我还在simple-talk找到了另一个有用的资源。
然而,在尝试使用此方法后,我仍然遇到同样的问题......从主视图回发后,EditorTemplate中的数据不会被保留。
在谷歌搜索后,我没有找到任何有用的信息。
我注意到简单说话示例使用了一个名为“editor-field”的div类,但IntelliSense并没有给我“编辑字段”作为选项...所以也许有一个我缺少的库或者什么?
那么......如何在主视图回传后让EditorTemplate保持不变?
非常感谢任何可以提供帮助的人。
答案 0 :(得分:0)
我已经提出了至少一个临时解决这个问题的方法,所以我现在告诉我现在做了什么才能让它发挥作用。但是,我认为这是一种黑客攻击,我应该以某种方式做得更好。所以,如果有人有任何信息我还在听。
所以基本上我创建了一个循环,它会强制页面在回发后刷新。我为Edit操作方法更改了控制器,以便在HttpPost成功时重定向到另一个名为RefreshInvoiceData的控制器操作方法:
req.getReader()
然后,RefreshInvoiceData操作方法重定向回Edit操作方法,并向其传递当前Invoice的ID。这有点让整个视图变得清爽。
// POST: DIM_Invoice/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "LPK_Invoice,InvoiceNumber,PONumber,InvoiceDate,OrderDate,ShipDate,Distributor,AllReceived,RowVersion,Comments")] DIM_Invoice dIM_Invoice)
{
if (ModelState.IsValid)
{
db.Entry(dIM_Invoice).State = EntityState.Modified;
db.SaveChanges();
//return View(dIM_Invoice);
//return RedirectToAction("Index");
return RedirectToAction("RefreshInvoiceData", new { invoiceId = dIM_Invoice.LPK_Invoice.ToString() });
}
return View(dIM_Invoice);
}
如果有人有更好的答案,我很乐意听到。