Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - MVC Model on Post has no navigation properties

I've looked around SO and I can't find anyone with an issue like this, so I'm posting this question.

I have a two edit actions in a controller (post and get) and in the GET I populate the Wine property of a viewmodel that gets passed to the view.

On the POST, I read that viewmodel, and save the changes to the database. After that I wanted to update my Lucene seach index with the updated Wine property. However, I keep getting an error because the Wine property doesn't have any of its navigation properties populated.

I tried looking up a new wine object using db.Wines.Find(ew.wine.WindID) but that still returns a Wine with no navigation properties. It is worth mentioning this is exactly what the GET edit action does, and the is populated correctly. I have no idea what is going on here. I also tried Wine w = db.Entry(ew.Wine).Entity to get those navigation properties populated, but to no avail... Thanks for the help in advance!

ViewModel & Wine Model

    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; }
}

Two Edit Actions from the controller:

 [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);
        }

    }

EDIT - here is the form in 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.Win

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...