Asp.net mvc 回发后不显示ASP.NET MVC 5 EditorTemplate

Asp.net mvc 回发后不显示ASP.NET MVC 5 EditorTemplate,asp.net-mvc,razor,mvc-editor-templates,Asp.net Mvc,Razor,Mvc Editor Templates,我有一个包含以下表格的数据库:DIM_Invoice、DIM_Carton、DIM_InvoiceItem。这是一个层次结构–发票包含一个或多个纸箱。纸箱包含一个或多个发票项目 这是我的ER图: 最后,我需要创建一个层次视图,它将显示发票信息、与发票相关的纸箱以及与每个纸箱相关的InvoiceItems。每个级别上都有几个字段应该是可编辑的 然而,现在,我只是想让前两个层次发挥作用:一张发票有多个纸箱 以下是实体框架生成的我的模型: namespace Invoices2.Models {

我有一个包含以下表格的数据库:DIM_Invoice、DIM_Carton、DIM_InvoiceItem。这是一个层次结构–发票包含一个或多个纸箱。纸箱包含一个或多个发票项目

这是我的ER图:

最后,我需要创建一个层次视图,它将显示发票信息、与发票相关的纸箱以及与每个纸箱相关的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_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>
@model Invoices2.Models.DIM\u纸箱
暗箱

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.LPK_) @LabelFor(model=>model.CartonNumber,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.CartonNumber,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.CartonNumber,“,new{@class=“text danger”})
最后,这里是DIM_Invoice的编辑视图,它试图通过调用@Html.EditorFor(model=>model.DIM_Carton)来利用DIM_Carton编辑器模板

@model Invoices2.Models.DIM\u发票
@{
ViewBag.Title=“编辑”;
}
编辑
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
发票

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.LPK\u发票) @LabelFor(model=>model.InvoiceNumber,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.InvoiceNumber,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.InvoiceNumber,“,new{@class=“text danger”}) @LabelFor(model=>model.PONumber,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.PONumber,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.PONumber,“,new{@class=“text danger”}) @LabelFor(model=>model.InvoiceDate,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.InvoiceDate,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.InvoiceDate,“,new{@class=“text danger”}) @LabelFor(model=>model.OrderDate,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.OrderDate,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.OrderDate,“,new{@class=“text danger”}) @LabelFor(model=>model.ShipDate,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ShipDate,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ShipDate,“,new{@class=“text danger”}) @LabelFor(model=>model.Distributor,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Distributor,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Distributor,“,new{@class=“text danger”}) @LabelFor(model=>model.AllReceived,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.AllReceived) @Html.ValidationMessageFor(model=>model.AllReceived,“,new{@class=“text danger”}) //***这是给编辑模板的电话*** @EditorFor(model=>model.DIM_) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
好的,我是MVC的新手,所以请容忍我。。。以下是我试图解决的具体问题:

首先,我尝试使用局部视图,其中DIM_Invoice Edit视图将DIM_Carton Edit视图视为局部视图。但是,每当在浏览器中对DIM_Invoice进行编辑并发回时,部分视图中显示的信息将不会被保留(即,由于缺少更好的描述,它只是从页面中“消失”)

在谷歌搜索之后,我发现了一个网站,上面说我应该使用EditorTemplates。这篇文章给了我很大的帮助,我还找到了另一个有用的资源

然而,在尝试使用这种方法之后,我仍然有同样的问题。。。从主视图回发后,EditorTemplate中的数据不会保留

在谷歌搜索了更多信息后,我没有找到任何有帮助的进一步信息

我没有注意到
@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>
@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")
}
// 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);
        }
// GET: DIM_Invoice
        public ActionResult RefreshInvoiceData(int? invoiceId)
        {
            if (invoiceId == null)
            {
                return Content("the invoiceId is null");
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            return RedirectToAction("Edit", new { id = invoiceId });
        }