I have a project in which I have a dropdown list which is bound to database. It is populating the values and my dropdown shows the values as well, but when I am submitting the form an error like below occurs:
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: items
I have bound the dropdown to the database but on post the controller is getting my class object which has a gender
property. I check that my model object has the correct, selected value from the dropdown, but when it moves to the statement modelstate.isvalid
it returns false.
public ActionResult Create(Registration registration)
{
if (ModelState.IsValid)
{
registration.ConfirmPassword = EncryptionLibrary.EncryptText(registration.ConfirmPassword);
registration.Password= EncryptionLibrary.EncryptText(registration.Password);
registration.Created_Date = DateTime.Now;
if (ModelState.IsValid)
{
db.Registration.Add(registration);
db.SaveChanges();
ViewBag.teacher = "Teacher account has been created Successfully";
return RedirectToAction("Create");
}
TempData["Faculty"] = "";
var result = _IRegistration.AddFaculty(registration);
if (result > 0)
{
TempData["Success"] = "Faculty Added Successfully";
ModelState.Clear();
return RedirectToAction("Create");
}
}
return View(registration);
}
Below is my razor syntax for the dropdown to bind the values from viewbag:
@Html.DropDownListFor(model=>model.Gender, new SelectList(ViewBag.Gender, "Id", "Name"), "Please select", new { @id = "ddlFruits",@class="btn form-text" })
I want to save the gender dropdown value into the database although it is getting populated then why it is saying Value cannot be null.
Parameter name: items