Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC3: View POSTed model value is NULL
    primarykey
    data
    text
    <p>I have the following controller and view. It is listing the values correctly using GET. When I click on the button it causes a POST. However the value received in the controller is NULL. How do we correct it?</p> <p>HIGHLIGHTED CODE</p> <pre><code> [HttpPost] public ActionResult CastVote(IEnumerable&lt;Program&gt; theProgramList) </code></pre> <p>GET Image</p> <p><img src="https://i.stack.imgur.com/n9z6p.jpg" alt="enter image description here"></p> <p>CODE</p> <pre><code> public enum RatingEnum { Poor = 0, Neutral, Good, Excellent }; public class Program { public int ProgramID { get; set; } public string ProgramName { get; set; } public RatingEnum RatingID { get; set; } public string ProgramCategory { get; set; } } </code></pre> <p>CONTROLLER</p> <pre><code>namespace MyProgramRatingApp.Controllers { public class ProgramController : Controller { List&lt;Program&gt; programList = new List&lt;Program&gt;() { new Program { ProgramID = 1,ProgramName = "Program1", ProgramCategory = "A" }, new Program { ProgramID = 2,ProgramName = "Program2", ProgramCategory = "B" }, new Program { ProgramID = 3,ProgramName = "Program3", ProgramCategory = "A" } }; // GET: /Program/ public ActionResult CastVote() { ViewBag.RatingEnum = GetRstingSelectList(); return View(programList); } // POST: /StoreManager/Create [HttpPost] public ActionResult CastVote(IEnumerable&lt;Program&gt; theProgramList) { if (ModelState.IsValid) { //Save the book in DB first and then redirectToAction. return RedirectToAction("CastVote"); } return View(theProgramList); } public static SelectList GetRstingSelectList() { Array values = Enum.GetValues(typeof(RatingEnum)); List&lt;System.Web.UI.WebControls.ListItem&gt; items = new List&lt;System.Web.UI.WebControls.ListItem&gt;(values.Length); foreach (var i in values) { items.Add(new System.Web.UI.WebControls.ListItem { Text = Enum.GetName(typeof(RatingEnum), i), Value = ((int)i).ToString() } ); } return new SelectList(items); } } } </code></pre> <p>VIEW</p> <pre><code>@model IEnumerable&lt;MyProgramRatingApp.Program&gt; @{ ViewBag.Title = "CastVote"; } &lt;h2&gt;CastVote&lt;/h2&gt; @using (Html.BeginForm()) { &lt;table&gt; &lt;tr&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramName &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; RatingID &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramCategory &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(modelItem =&gt; item.ProgramName) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(modelItem =&gt; item.RatingID) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(modelItem =&gt; item.ProgramCategory) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DropDownListFor(model =&gt; item.RatingID, (SelectList)ViewBag.RatingEnum, String.Empty) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Cast Vote" /&gt; &lt;/p&gt; } </code></pre> <p>READING:</p> <ol> <li><p><a href="https://stackoverflow.com/questions/6807256/dropdownlist-set-selected-value-in-mvc3-razor">dropdownlist set selected value in MVC3 Razor</a></p></li> <li><p><a href="https://stackoverflow.com/questions/5037580/asp-net-mvc-3-partial-vs-display-template-vs-editor-template">ASP.NET MVC 3 - Partial vs Display Template vs Editor Template</a></p></li> <li><p><a href="https://stackoverflow.com/questions/9005785/ienumerable-property-with-mvc3-editortemplate">IEnumerable property with MVC3 EditorTemplate</a></p></li> <li><p>ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries</p></li> <li><p>Model Binding To A List</p></li> </ol> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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