I'm trying to get the selected value of a dropdown from the Edit view to the controller. I keep getting the same error "{"Value cannot be null.\r\nParameter name: items"}"
Here is my view:
<div class="form-group">
@Html.LabelFor(model => model.adminID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.adminID, new SelectList(ViewBag.Admins, "Value", "Text"), htmlAttributes: new { @class = "form-control", @name = "admin" })
@Html.ValidationMessageFor(model => model.adminID, "", new { @class = "text-danger" })
</div>
</div>
I created the SelectList in the get action.
var admins = from a in db.members
select new { name = a.student.Fname + " " + a.student.Sname, id = a.StudentID };
ViewBag.Admins = admins.Select(a => new SelectListItem
{
Text = a.name,
Value = a.id
}).ToList();
And here is the controller. I used adminID, the name of the property to try bind it but it returns the error when I post the page.
public async Task<ActionResult> Edit([Bind(Include = "ClubId,ClubName,CreationDate,adminID")] Club club)
{
if (ModelState.IsValid)
{
db.Entry(club).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(club);
}
The Error being returned is
"{"Value cannot be null.\r\nParameter name: items"}"
Any ideas why the adminID won't save/bind?
Route Config
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Clubs", action = "Index", id = UrlParameter.Optional }
);
}
}