MVC5 DropDownList: There is no ViewData item of type ‘IEnumerable‘ that has the key ‘Distributor’

I'm having trouble interacting with a dropdownlist in an MVC 5 project.


 // GET: /Account/Register [AllowAnonymous] public ActionResult Register() { PhoenixMachineTrackingEntities db = new PhoenixMachineTrackingEntities(); var model = new RegisterViewModel(); List<SelectListItem> distList = new List<SelectListItem>(); foreach(Distributor d in db.Distributor) { distList.Add(new SelectListItem { Text = d.Dist_Name, Value = d.Dist_ID.ToString() }); } model.DistributorList = distList; return View(model); } // // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var test = model.Distributor; var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Distributor = model.Distributor}; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); } 


 public class RegisterViewModel { [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } public IEnumerable<System.Web.Mvc.SelectListItem> DistributorList{get; set;} [Required] public Distributor Distributor { get; set; } } 


 @model PhoenixProductTracking.Models.RegisterViewModel @{ ViewBag.Title = "Register"; } <h2>@ViewBag.Title.</h2> @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <h4>Create a new account.</h4> <hr /> @Html.ValidationSummary("", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Distributor, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.DropDownListFor(m => m.Distributor, Model.DistributorList, "---Please Select Distributor---") </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" class="btn btn-default" value="Register" /> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } 

When I open the page in question (registration page) the drop down list is populated with the correct information and the "must be selected" validation works perfectly. But once I choose a valid distributor from the dropdown and press register I get the error in my title.

I suspect that this is because the page isn't properly communicating back to the controller what the selection was. I thought (for a moment) that changing the "value" property of the SelectedListItem object to "Distributor" would satisfy it, but unless I choose a string object, it throws an error, so that doesn't appear to be an option.

Am I doing something wrong, or just missing a piece of the puzzle here? My view is (should be?) strongly typed, and that seems to be confirmed by my ability to populate the dropdown box without using the viewbag.

I've read that this can happen due to a collection being null, but when i break the list in the controller, just before it returns View(model) both model.DistributorList and distList have a count of 5, and (as I said) the dropdown is being poulated

by Pidgey_OP via /r/csharp

Leave a Reply