Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 DropDownListFor error: Null Values
    primarykey
    data
    text
    <p>I am a beginner programmer and having trouble with the @Html.DropDownListFor helper...</p> <p>I am using a General Repository and Unit of Work pattern based off of the tutorial here: <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow noreferrer">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application</a></p> <p>Here is my code for the Repository:</p> <pre><code>public class GenericRepository&lt;TEntity&gt; where TEntity : class { internal UsersContext context; internal DbSet&lt;TEntity&gt; dbSet; public GenericRepository(UsersContext context) { this.context = context; this.dbSet = context.Set&lt;TEntity&gt;(); } public virtual IEnumerable&lt;TEntity&gt; Get( Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null, string includeProperties = "") { IQueryable&lt;TEntity&gt; query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } public virtual TEntity GetByID(object id) { return dbSet.Find(id); } public virtual void Insert(TEntity entity) { dbSet.Add(entity); } // Delete methods not shown public virtual void Update(TEntity entityToUpdate) { dbSet.Attach(entityToUpdate); context.Entry(entityToUpdate).State = EntityState.Modified; } } </code></pre> <p>Here is my code for my UnitOfWork class:</p> <pre><code>public class UnitOfWork : IDisposable { private UsersContext context = new UsersContext(); private GenericRepository&lt;UserProfile&gt; userProfileRepository; private GenericRepository&lt;Lead&gt; leadRepository; private GenericRepository&lt;UnitedStatesState&gt; unitedStatesStateRepository; public GenericRepository&lt;UserProfile&gt; UserProfileRepository { get { if (this.userProfileRepository == null) { this.userProfileRepository = new GenericRepository&lt;UserProfile(context); } return userProfileRepository; } } public GenericRepository&lt;Lead&gt; LeadRepository { get { if (this.leadRepository == null) { this.leadRepository = new GenericRepository&lt;Lead&gt;(context); } return leadRepository; } } public GenericRepository&lt;UnitedStatesState&gt; UnitedStatesStateRepository { get { if (this.unitedStatesStateRepository == null) { this.unitedStatesStateRepository = new GenericRepository&lt;UnitedStatesState&gt;(context); } return unitedStatesStateRepository; } } </code></pre> <p>I am trying to use strongly typed views and models in order to pass the selectlist data to the view without using ViewData/ViewBag. From what I understand, the best practice is to do something similar to what I saw here: <a href="https://stackoverflow.com/questions/4729440/validate-a-dropdownlist-in-asp-net-mvc">validate a dropdownlist in asp.net mvc</a></p> <p>I tried following that as closely as possible and this is what I came up with</p> <p>My View Model looks like this:</p> <pre><code>public class Lead { public int LeadID { get; set; } public int CompanyID { get; set; } [Required(ErrorMessage = "Please enter state")] [Display(Name = "State")] [MaxLength(2)] public string State { get; set; } [Display(Name = "Assigned To")] public string AssignedTo { get; set; } [Timestamp] public Byte[] Timestamp { get; set; } public virtual Company Company { get; set; } // IEnumerables for Dropdown Lists passed to views public IEnumerable&lt;UnitedStatesState&gt; UnitedStatesStates { get; set; } public IEnumerable&lt;UserProfile&gt; UserProfiles { get; set; } // Objects passed to views public Lead lead { get; set; } } </code></pre> <p>These IEnumerables for my dropdown lists are then populated in my controller from my database through my repository. The odd part is that I am using these dropdown lists in two different views, Create and Edit. When I use the dropdown lists in the Create view they work perfectly both on the GET and POST ActionResults. When I try and use the same dropdown lists for my Edit view they work for the GET ActionResult (the view loads and the dropdowns work) but when I try to POST them to my Edit ActionResult I get the following error:</p> <p>{"Value cannot be null.\r\nParameter name: items"} // This is the error as shown in Visual Studio 2012</p> <p>System.ArgumentNullException: Value cannot be null. Parameter name: items // This is the error shown in Google Chrome</p> <p>Below is my Lead Controller with the Edit and Create ActionResults:</p> <pre><code>public class LeadController : Controller { // create instance of Repository Unit of Work private UnitOfWork unitOfWork = new UnitOfWork(); public ActionResult Create() { // Get the current users profile UserProfile userProfile = UserProfile.GetCurrentUserProfile(); // Creates Dropdown Lists to pass to view var model = new Lead { UnitedStatesStates = unitOfWork.UnitedStatesStateRepository.Get(u =&gt; u.StateAbbreviation != null), UserProfiles = unitOfWork.UserProfileRepository.Get(u =&gt; u.CompanyID == userProfile.CompanyID) }; // Return View return View(model); } [HttpPost] public ActionResult Create(Lead model) { try { if (ModelState.IsValid) { // Call the current users profile UserProfile userProfile = UserProfile.GetCurrentUserProfile(); // Create a new lead and apply all attirbutes that were entered Lead lead = new Lead(); lead.CompanyID = userProfile.CompanyID; lead.State = model.State; lead.AssignedTo = model.AssignedTo; // Add the lead and save the changes. Redirect to Lead Index. unitOfWork.LeadRepository.Insert(lead); unitOfWork.Save(); return RedirectToAction("Index"); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again and if the problem persists, see your system administrator."); } // Return view if ModelState is not valid return View(); } public ActionResult Edit(int id = 0) { // Get Users Profile UserProfile userProfile = UserProfile.GetCurrentUserProfile(); // Check to see if Lead Exists if (unitOfWork.LeadRepository.GetByID(id) == null) { return HttpNotFound(); } // Creates Dropdown Lists and Gets current lead values to pass to view var model = new Lead { lead = unitOfWork.LeadRepository.GetByID(id), UnitedStatesStates = unitOfWork.UnitedStatesStateRepository.Get(u =&gt; u.StateAbbreviation != null), UserProfiles = unitOfWork.UserProfileRepository.Get(u =&gt; u.CompanyID == userProfile.CompanyID) }; return View(model); } [HttpPost] public ActionResult Edit(Lead lead) { try { // Update lead if model state is valid if (ModelState.IsValid) { unitOfWork.LeadRepository.Update(lead); unitOfWork.Save(); return RedirectToAction("Index"); } } // Catch any concurrency exceptions catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var databaseValues = (Lead)entry.GetDatabaseValues().ToObject(); var clientValues = (Lead)entry.Entity; if (databaseValues.State != clientValues.State) ModelState.AddModelError("State", "Current value: " + databaseValues.State); if (databaseValues.AssignedTo != clientValues.AssignedTo ) ModelState.AddModelError("Assigned To ", "Current value: " + databaseValues.AssignedTo ); ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); lead.Timestamp = databaseValues.Timestamp; } catch (DataException) { //Log the error (add a variable name after Exception) ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator."); } // Return View if Model State is not valid return View(lead); } </code></pre> <p>The POST Edit ActionResult includes code to catch concurrencies which I created following the tutorial shown here: <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow noreferrer">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application</a></p> <p>Below is my view for Create (this works perfectly):</p> <pre><code>@model SolarToolbase.Models.Lead @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.State) @Html.DropDownListFor(model =&gt; model.State, new SelectList(Model.UnitedStatesStates, "StateAbbreviation", "UnitedStatesStateName"),"Choose State")&lt;br /&gt; @Html.ValidationMessageFor(model =&gt; model.State) &lt;/div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.AssignedTo) @Html.DropDownListFor(model =&gt; model.AssignedTo, new SelectList(Model.UserProfiles, "FullName", "FullName"),"Choose User")&lt;br /&gt; @Html.ValidationMessageFor(model =&gt; model.AssignedTo) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/div&gt; } </code></pre> <p>Below is my view for Edit(this throws the aforementioned errors when I hit the submit button. I inserted a comment below to show the line that the error is being thrown from):</p> <pre><code>@model SolarToolbase.Models.Lead @using (Html.BeginForm()) { @Html.ValidationSummary(true) @Html.HiddenFor(model =&gt; model.lead.LeadID) @Html.HiddenFor(model =&gt; model.lead.Timestamp) &lt;div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.lead.State) @Html.DropDownListFor(model =&gt; model.lead.State, new SelectList(Model.UnitedStatesStates, "StateAbbreviation", "UnitedStatesStateName"))&lt;br /&gt; // Error thrown from this line @Html.ValidationMessageFor(model =&gt; model.lead.State) &lt;/div&gt; &lt;div&gt; @Html.LabelFor(model =&gt; model.lead.AssignedTo) @Html.DropDownListFor(model =&gt; model.lead.AssignedTo, new SelectList(Model.UserProfiles, "FullName", "FullName"))&lt;br /&gt; @Html.ValidationMessageFor(model =&gt; model.lead.AssignedTo) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;/div&gt; } </code></pre> <p>I apologize in advance for posting so much code, I just honestly don't know where this error is coming from and I've beat my head against the wall trying to figure it out for about 4 hours now. Free virtual high fives and good karma for anyone that can help.</p> <p>Thanks!</p>
    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.
    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