Note that there are some explanatory texts on larger screens.

plurals
  1. POASP MVC View posts null to Controller
    text
    copied!<p>I am a beginner in ASP MVC, and, after a lot of help from SO, am progressing through ViewModels. Using a ViewModel however, I have encountered the following error.</p> <p>Given the following View:</p> <pre><code>@model November.ViewModels.Staff_Salutation_VM //... using (Html.BeginForm("UpdateStaff", "Settings", FormMethod.Post, new { @class = "clearfix parameter-form update-parameter update-staff", enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) //... @for (int i = 0; i &lt; Model.AvailableStaffMembers.Count; i++) { var staff = Model.AvailableStaffMembers[i]; &lt;tr&gt; &lt;td&gt;@Html.HiddenFor(model =&gt; staff.ID)@Html.ValueFor(model =&gt; staff.ID)&lt;/td&gt; &lt;td&gt; @Html.DropDownListFor( model =&gt; model.SalutationID, Model.AvailableSalutations.Select(option =&gt; new SelectListItem { Text = option.Desc.ToString(), Value = option.ID.ToString(), Selected = (option.ID.ToString() == staff.SalutationID.ToString()) } ), "Choose...") &lt;/td&gt; &lt;td&gt;@Html.EditorFor(model =&gt; staff.FName)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(model =&gt; staff.LName)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(model =&gt; staff.Active)&lt;/td&gt; &lt;td&gt;&lt;a href="/Settings/DeleteStaff?id=@Html.ValueFor(model =&gt; staff.ID)"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>and the following Controller:</p> <pre><code> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using November.Models; using November.ViewModels; using November.DAL; //... //GET var staffCreateViewModel = new Staff_Salutation_VM(); staffCreateViewModel.AvailableSalutations = new List&lt;Prm_Salutation&gt;(); var activeSalts = (from a in db.Prm_Salutations where a.Active == true orderby a.Desc ascending select a); staffCreateViewModel.AvailableSalutations = activeSalts.ToList(); staffCreateViewModel.AvailableStaffMembers = new List&lt;Prm_Staff&gt;(); var activeStaff = (from a in db.Prm_Staffs where a.Active == true orderby a.LName ascending select a); staffCreateViewModel.AvailableStaffMembers = activeStaff.ToList(); return View("StaffMembers", staffCreateViewModel); //POST public ActionResult UpdateStaff(Staff_Salutation_VM list) { if (ModelState.IsValid) { foreach (var formData in list) //no longer works due to dropping List&lt;&gt; { var tbl = db.Prm_Staffs.Where(a =&gt; a.ID.Equals(formData.ID)).FirstOrDefault(); if (tbl != null) { var Prm_StaffModel = new Prm_Staff(); Prm_StaffModel.SalutationID = formData.SalutationID; Prm_StaffModel.FName = formData.FName; Prm_StaffModel.LName = formData.LName; Prm_StaffModel.Active = formData.Active; } } db.SaveChanges(); ViewBag.UpdateRtrn = "Successfully Updated."; return RedirectToAction("Parameters", new { param = "Staff Members" }); } else { ViewBag.UpdateRtrn = "Failed ! Please try again."; return RedirectToAction("Parameters", new { param = "Staff Members" }); } } return RedirectToAction("Parameters", new { param = "Staff Members" }); } </code></pre> <p>And, for good measure, the ViewModel itself:</p> <pre><code> public class Staff_Salutation_VM { public int ID { get; set; } public int SalutationID { get; set; } public string FName { get; set; } public string LName { get; set; } public bool Active { get; set; } public List&lt;Prm_Salutation&gt; AvailableSalutations { get; set; } public List&lt;Prm_Staff&gt; AvailableStaffMembers { get; set; } public Staff_Salutation_VM() { } } </code></pre> <p>When triggered, no form values populate the ActionResult, resulting in a <code>Object reference not set to an instance of an object.</code> exception being thrown when the <code>foreach (var formData in list)</code> line is reached. Debugging shows <code>list</code> as being null. How can this be so? Or rather, what am I doing wrong?</p> <p>EDIT: the <code>list</code> variable in my POST <code>ActionResult</code> is now getting data - or at least, is showing the various types in the class when debugged. How do I then iterate through it to save that data in the appropriate rows of the DB?</p>
 

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