Post上的MVC模型没有导航属性

时间:2012-08-03 18:21:14

标签: asp.net-mvc-3 entity-framework-4

我环顾四周,找不到有这样问题的人,所以我发布了这个问题。

我在控制器中有两个编辑操作(post和get),在GET中我填充了传递给视图的viewmodel的Wine属性。

在POST上,我读取了viewmodel,并将更改保存到数据库中。之后我想用更新的Wine属性更新我的Lucene搜索索引。但是,我一直收到错误,因为Wine属性没有填充任何导航属性。

我尝试使用db.Wines.Find(ew.wine.WindID)查找新的wine对象,但仍然返回没有导航属性的Wine。值得一提的是,这正是GET编辑操作的作用,并且填充正确。我不知道这里发生了什么。我也试过Wine w = db.Entry(ew.Wine).Entity来填充那些导航属性,但无济于事...感谢您的帮助!

ViewModel&葡萄酒模型

    public class NewWineViewModel
    {

        public Wine Wine { get; set; }
        public VOAVIRequest VOAVIRequest { get; set; }
        public bool IsRequest { get; set; }

        public SelectList VarTypes { get; set; }
        public SelectList Origins { get; set; }
        public SelectList Apps { get; set; }
        public SelectList Vintages { get; set; }
        public SelectList Importers { get; set; }

        public NewWineViewModel()
        {
            this.Wine = new Wine();
        }

    }

 public class Wine :Updater
    {
        public int WineID { get; set; }
        //public int WineTypeID { get; set; }
        [Display(Name = "Varietal/Type")]
        public int? VarTypeID { get; set; }
        [Display(Name = "Origin")]
        public int? OriginID { get; set; }
        [Display(Name = "Appellation")]
        public int? AppID { get; set; }
        [Display(Name = "Vintage")]
        public int? VintageID { get; set; }
        [Display(Name = "Importer")]
        public int? ImporterID { get; set; }
        public int ProducerID { get; set; }
        public string Designate { get; set; }
        [Display(Name = "Drink Window")]
        public string DrinkWindow { get; set; }
        public string Body { get; set; }
        public string SKU { get; set; }
        [Display(Name = "Varietal Makeup")]
        public string VarietalMakeup { get; set; }
        [Display(Name = "Case Production")]
        public string CaseProduction { get; set; }
        [Display(Name = "Alcohol Content")]
        public double? AlcoholContent { get; set; }
        public string Winemaker { get; set; }
        [Display(Name = "Consulting Winemaker")]
        public string ConsultWinemaker { get; set; }
        public bool Sustainable { get; set; }
        public bool Kosher { get; set; }
        public bool Organic { get; set; }
        public bool Biodynamic { get; set; }
        public bool SalmonSafe { get; set; }
        public Boolean Active { get; set; }
        [Display(Name = "ResidualSugar")]
        public double? RS { get; set; }
        public double? pH { get; set; }
        public string QRUrl { get; set; }


        public virtual WineType WineType { get; set; }

        public virtual VarType VarType { get; set; }
        public virtual Origin Origin { get; set; }
        public virtual App App { get; set; }
        public virtual Vintage Vintage { get; set; }
        public virtual Importer Importer { get; set; }
        public virtual Producer Producer { get; set; }
}

来自控制器的两个编辑操作:

 [ProducerEdit]
    public ActionResult Edit(int WineID)
    {

        NewWineViewModel ew = new NewWineViewModel();
        ew.Wine = db.Wines.Find(WineID);
        ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
        ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
        ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
        ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
        //might need to handle null on this one
        ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
        return View(ew);

    }

    //post
    [HttpPost]
    [ProducerEdit]
    public ActionResult Edit(NewWineViewModel ew)
    {

        if (ModelState.IsValid)
        {
            ew.Wine.Body = ew.Wine.Body == "Please select wine body" ? string.Empty : ew.Wine.Body;

            ew.Wine.UpdatedBy = User.Identity.Name;
            ew.Wine.UpdatedOn = DateTime.Now;
            db.Entry(ew.Wine).State = EntityState.Modified;
            db.SaveChanges();

            Wine w = db.Wines.Find(ew.Wine.WineID);


            Lucene.LuceneSearch.ClearLuceneIndexRecord(ew.Wine.WineID);
            Lucene.LuceneSearch.AddUpdateLuceneIndex(ew.Wine);

            if (ew.IsRequest)
            {
                ew.VOAVIRequest.WineID = ew.Wine.WineID;
                ew.VOAVIRequest.CreatedBy = User.Identity.Name;
                ew.VOAVIRequest.CreatedOn = DateTime.Now;
                db.VOAVIRequests.Add(ew.VOAVIRequest);
                db.SaveChanges();

                return RedirectToAction("Requested");

                //redirect to "Request Submitted" page for new wines
            }

            return RedirectToAction("Details", new { id = ew.Wine.WineID });

        }
        else
        {
            ew.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name", ew.Wine.VarTypeID);
            ew.Origins = new SelectList(db.Origins, "OriginID", "Name", ew.Wine.OriginID);
            ew.Apps = new SelectList(db.Apps, "AppID", "Name", ew.Wine.AppID);
            ew.Vintages = new SelectList(db.Vintages, "VintageID", "Name", ew.Wine.VintageID);
            //might need to handle null on this one
            ew.Importers = new SelectList(db.Importers, "ImporterID", "Name", ew.Wine.ImporterID);
            return View(ew);
        }

    }

编辑 - 这是Razor中的表格

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <h3>@ViewBag.ProducerName</h3>
    @Html.HiddenFor(m => m.Wine.WineID)
    @Html.HiddenFor(m => m.Wine.ProducerID)
    @Html.HiddenFor(m => m.IsRequest)

    <h4 style="margin-top: -40px; padding-bottom: 10px;">Editing @Model.Wine.Name</h4>
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.Wine.VarTypeID)
            </td>
            <td style="display: inline">
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.VarTypeID, Model.VarTypes, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.VarType, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewVar" class="filetypes" href="#">New Varietal?</a> @*                @Html.ValidationMessageFor(m => m.VOAVIRequest.VarType)*@
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.OriginID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.OriginID, Model.Origins, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Origin, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewOrigin" class="filetypes" href="#">New Origin?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.AppID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.AppID, Model.Apps, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.App, new { style = "display: none;", @class = "voavignore" })<a
                    id="lnkNewApp" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.VintageID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.VintageID, Model.Vintages, new { @class = "chzn-select" })
                </div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Vintage, new { style = "display: none;", @class = "voavignore" })
                <a id="lnkNewVintage" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Designate)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Designate)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.DrinkWindow)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.DrinkWindow)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.VarietalMakeup)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.VarietalMakeup)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Body)
            </td>
            <td>
                @Html.DropDownListFor(m => m.Wine.Body, new SelectList(Model.Wine.BodyList, "Text", "Text", Model.Wine.Body), new { @class = "chzn-select" })
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.ImporterID)
            </td>
            <td>
                <div class="voavi-select">
                    @Html.DropDownListFor(m => m.Wine.ImporterID, Model.Importers, new { @class = "chzn-select" })</div>
                @Html.TextBoxFor(m => m.VOAVIRequest.Importer, new { style = "display: none;" })
                <a id="lnkNewImporter" class="filetypes" href="#">New Varietal?</a>
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.SKU)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.SKU)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.CaseProduction)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.CaseProduction)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.AlcoholContent)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.AlcoholContent)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.RS)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.RS)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.pH)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.pH)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Winemaker)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Winemaker)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.ConsultWinemaker)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.ConsultWinemaker)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Sustainable)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Sustainable)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Kosher)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Kosher)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Organic)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Organic)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.Biodynamic)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.Biodynamic)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(m => m.Wine.SalmonSafe)
            </td>
            <td>
                @Html.EditorFor(m => m.Wine.SalmonSafe)
            </td>
        </tr>
    </table>
    <p>
        <input type="submit" value="Update" />
    </p>
}

0 个答案:

没有答案