Note that there are some explanatory texts on larger screens.

plurals
  1. POModel not retaining value in MVC3 on Post
    text
    copied!<p>I'm working a viewmodel (vm) for creating a new wine. I assign the ProducerID value to the vm on the get based on the user's profile. I can see the ProducerID value in the view when it is rendered in the view. The user cannot choose or edit this value unless they are in the admin role (i'm not testing with that role). My issue is the ProducerID always comes back on the POST as 0. I don't know what I'm missing as my other selected options in the view come back fine. </p> <p>I've tried to put a new unique name in the vm itself, but that didn't hold a value either. I've look around and found some other people with similar issues, but none of their solutions have helped. Any assistance on this would be awesome. Thanks! </p> <p>viewmodel:</p> <pre><code>{ 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(); } } </code></pre> <p>wine model:</p> <pre><code>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 = "Case Production")] public double 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; } 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; } public virtual ICollection&lt;Review&gt; Reviews { get; set; } public virtual ICollection&lt;Doc&gt; Docs { get; set; } public IEnumerable&lt;SelectListItem&gt; BodyList { get; set; } //for dropdownlist binding //public IEnumerable&lt;VarType&gt; VarTypes { get; set; } //public IEnumerable&lt;Origin&gt; Origins { get; set; } //public IEnumerable&lt;App&gt; Apps { get; set; } //public IEnumerable&lt;Vintage&gt; Vintages { get; set; } //public IEnumerable&lt;Importer&gt; Importers { get; set; } //public IEnumerable&lt;Producer&gt; Producers { get; set; } public Wine() { var BodyList = new List&lt;SelectListItem&gt;() { new SelectListItem {Value="", Text="Please select wine body"}, new SelectListItem {Value="", Text="Light-bodied"}, new SelectListItem {Value="", Text="Light to Medium-bodied"}, new SelectListItem {Value="", Text="Medium-bodied"}, new SelectListItem {Value="", Text="Medium to Full-bodied"}, new SelectListItem {Value="", Text="Full-bodied"}, new SelectListItem {Value="", Text="Very Full-bodied"} }; this.BodyList = BodyList; } public virtual String Name { get { string sName = string.Empty; int iVintage; if (!int.TryParse(this.Vintage.Name.Trim(), out iVintage)) { sName = iVintage.ToString(); } if (!string.IsNullOrEmpty(this.Designate)) { sName = sName + " " + this.Producer.Name + " " + this.Designate + " " + this.VarType.Name; } else { sName = sName + " " + this.Producer.Name + " " + this.VarType.Name; } return sName; } } } </code></pre> <p>controller:</p> <pre><code> public ActionResult Create() { NewWineViewModel nw = new NewWineViewModel(); nw.VarTypes = new SelectList(db.VarTypes, "VarTypeID", "Name").Default("Select a Varietal/Type", "0"); nw.Origins = new SelectList(db.Origins, "OriginID", "Name").Default("Select an Origin", "0"); nw.Apps = new SelectList(db.Apps, "AppID", "Name").Default("Select an Appellation", "0"); nw.Vintages = new SelectList(db.Vintages, "VintageID", "Name").Default("Select a Vintage", "0"); nw.Importers = new SelectList(db.Importers, "ImporterID", "Name").Default("Select an Importer", "0"); // keep dynamic if (User.IsInRole("producer")) { Producer currentProd = db.ProducerUsers.Find(Membership.GetUser().ProviderUserKey).Producer; nw.Wine.ProducerID = currentProd.ProducerID; ViewBag.ProducerName = currentProd.Name; ViewBag.ProducerID = currentProd.ProducerID; } else { ViewBag.ProducerSelect = new SelectList(db.Producers, "ProducerID", "Name"); } ViewData.Model = nw; return View(); } // // POST: /Wine/Create [HttpPost] //[Authorize(Roles = "admin, producereditor")] public ActionResult Create(NewWineViewModel nw) { if (ModelState.IsValid) { nw.Wine.Active = nw.IsRequest ? false : true; nw.Wine.ImporterID = nw.Wine.ImporterID == 0 ? null : nw.Wine.ImporterID; nw.Wine.CreatedBy = this.User.Identity.Name; nw.Wine.CreatedOn = DateTime.Now; db.Wines.Add(nw.Wine); db.SaveChanges(); if (nw.IsRequest) { nw.VOAVIRequest.WineID = nw.Wine.WineID; db.VOAVIRequests.Add(nw.VOAVIRequest); RedirectToAction("Requested"); //redirect to "Request Submitted" page for new wines } return RedirectToAction("Details", nw.Wine.WineID); } ViewBag.VarTypeID = new SelectList(db.VarTypes, "VarTypeID", "Name").Default("Select a Varietal/Type", nw.Wine.VarTypeID.ToString()); ViewBag.OriginID = new SelectList(db.Origins, "OriginID", "Name").Default("Select an Origin", nw.Wine.OriginID.ToString()); ViewBag.AppID = new SelectList(db.Apps, "AppID", "Name").Default("Select an Appellation", nw.Wine.AppID.ToString()); ViewBag.VintageID = new SelectList(db.Vintages, "VintageID", "Name").Default("Select a Vintage", nw.Wine.VintageID.ToString()); ViewBag.ImporterID = new SelectList(db.Importers, "ImporterID", "Name").Default("Select an Importer", nw.Wine.ImporterID.ToString()); if (User.IsInRole("producer")) { Producer currentProd = db.ProducerUsers.Find(Membership.GetUser().ProviderUserKey).Producer; ViewBag.ProducerID = currentProd.ProducerID; ViewBag.ProducerName = currentProd.Name; } else { ViewBag.ProducerSelect = new SelectList(db.Producers, "ProducerID", "Name" ,nw.Wine.ProducerID); } return View(nw); } </code></pre> <p>view:</p> <pre><code>@model vf2.ViewModels.NewWineViewModel @{ ViewBag.Title = "Create a Wine"; } @using (Html.BeginForm()) { @Html.ValidationSummary(true) if (User.IsInRole("admin")) { &lt;div class="editor-label"&gt; @Html.LabelFor(m =&gt; m.Wine.ProducerID, "Producer") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.Wine.ProducerID, ViewBag.ProducerSelect as SelectList, "Select a Varietal/Type") @*@Html.DropDownList("ProducerSelect", String.Empty)*@ &lt;/div&gt; } else { &lt;h3&gt;@ViewBag.ProducerName&lt;/h3&gt; } @Html.HiddenFor(m =&gt; m.IsRequest) &lt;table&gt; &lt;tr&gt; &lt;td&gt;@Html.LabelFor(m =&gt; m.Wine.VarTypeID, "VarType") &lt;/td&gt; &lt;td&gt; &lt;div class="voavi-select"&gt; @Html.DropDownListFor(m =&gt; m.Wine.VarTypeID, Model.VarTypes, new { @class = "chzn-select" }) &lt;/div&gt; @Html.TextBoxFor(m =&gt; m.VOAVIRequest.VarType, new { style = "display: none;", @class = "voavignore" }) &lt;a id="lnkNewVar" class="filetypes" href="#"&gt;New Varietal?&lt;/a&gt; @* @Html.ValidationMessageFor(m =&gt; m.VOAVIRequest.VarType)*@ &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.OriginID, "Origin") &lt;/td&gt; &lt;td&gt; &lt;div class="voavi-select"&gt; @Html.DropDownListFor(m =&gt; m.Wine.OriginID, Model.Origins, new { @class = "chzn-select" }) &lt;/div&gt; &lt;a id="lnkNewOrigin" class="filetypes" href="#"&gt;New Origin?&lt;/a&gt; @Html.TextBoxFor(m =&gt; m.VOAVIRequest.Origin, new { style = "display: none;", @class = "voavignore" }) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.AppID, "App") &lt;/td&gt; &lt;td&gt; &lt;div class="voavi-select"&gt; @Html.DropDownListFor(m =&gt; m.Wine.AppID, Model.Apps, new { @class = "chzn-select" }) &lt;/div&gt; &lt;a id="lnkNewApp" class="filetypes" href="#"&gt;New Varietal?&lt;/a&gt; @Html.TextBoxFor(m =&gt; m.VOAVIRequest.App, new { style = "display: none;", @class = "voavignore" }) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.VintageID, "Vintage") &lt;/td&gt; &lt;td&gt; &lt;div class="voavi-select"&gt; @Html.DropDownListFor(m =&gt; m.Wine.VintageID, Model.Vintages, new { @class = "chzn-select" }) &lt;/div&gt; &lt;a id="lnkNewVintage" class="filetypes" href="#"&gt;New Varietal?&lt;/a&gt; @Html.TextBoxFor(m =&gt; m.VOAVIRequest.Vintage, new { style = "display: none;", @class = "voavignore" }) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Designate) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Designate) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.DrinkWindow) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.DrinkWindow) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Body) &lt;/td&gt; &lt;td&gt; @Html.DropDownListFor(m =&gt; m.Wine.Body, new SelectList(Model.Wine.BodyList, "Value", "Text"), new { @class = "chzn-select" }) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.ImporterID, "Importer") &lt;/td&gt; &lt;td&gt; &lt;div class="voavi-select"&gt; @Html.DropDownListFor(m =&gt; m.Wine.ImporterID, Model.Importers, new { @class = "chzn-select" })&lt;/div&gt; &lt;a id="lnkNewImporter" class="filetypes" href="#"&gt;New Varietal?&lt;/a&gt; @Html.TextBoxFor(m =&gt; m.VOAVIRequest.Importer, new { style = "display: none;" }) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.SKU) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.SKU) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.CaseProduction) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.CaseProduction) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.AlcoholContent) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.AlcoholContent) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Winemaker) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Winemaker) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.ConsultWinemaker) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.ConsultWinemaker) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Sustainable) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Sustainable) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Kosher) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Kosher) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Organic) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Organic) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.Biodynamic) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.Biodynamic) &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; @Html.LabelFor(m =&gt; m.Wine.SalmonSafe) &lt;/td&gt; &lt;td&gt; @Html.EditorFor(m =&gt; m.Wine.SalmonSafe) &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload