I know this question has been asked already here in SO, but I can't distinguish my error from this post : DropDownListFor Not Selecting Value
I did not use ViewBag's name the same as the model's property coming from the link.
Here is my code in the controller:
public ActionResult Create()
{
ViewBag.Companies = db.Companies.ToList();
return View();
}
and this is my code in the view:
<div class="editor-field">
@Html.DropDownListFor(model => model.ParentId, new SelectList(ViewBag.Companies, "Id", "ShortName"), "Select a Category")
@Html.ValidationMessageFor(model => model.ParentId)
</div>
in my create form, the dropdown list generates without error, however after clicking create, an error states "value cannot be null" even though I have selected a value.
I even added the ViewBag again in the [POST]Create Action :
[HttpPost]
public ActionResult Create(Company company)
{
ViewBag.Companies = db.Companies.ToList();
if (ModelState.IsValid)
{
db.Companies.AddObject(company);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(company);
}
I don't know where the error resides, I am completely new to ASP.NET MVC.
Here is the error log: