I am trying to populate an ASP MVC3 drop down list with only two select-able items and determine which item to select based on a value in a database. So far, this is what I have in my View (which I believe is correct)
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.DropDownList("CategoryID")
</div>
But I am running into a problem getting and setting the values in the controller.
First, the method I am using seems to only select the value in the drop down list according to which SelectListItem is listed first.
public ActionResult Edit(int id)
{
//redirect if security is not met. Must be admin here
if (!Security.IsAdmin(User)) return RedirectToAction("Message", "Home", new { id = 1 });
var item = db.FollowUpItems.Find(id);
string start = string.Empty;
if (item.Status.Equals("O")) start = "Open";
else start = "Closed";
ViewBag.CategoryID = new SelectList(new[]
{
new SelectListItem {Text = "O", Value = "Open"},
new SelectListItem {Text = "C", Value = "Closed"},
},"Text", "Value", start);
FollowUpItems followupitems = db.FollowUpItems.Find(id);
return View(followupitems);
}
Second, when I hit the Save button on the edit page, the Status property of the FollowUpItem that gets passed into this part of the controller is always null.
[HttpPost]
public ActionResult Edit(FollowUpItems followupitems)
{
try
{
if (ModelState.IsValid)
{
db.Entry(followupitems).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DbEntityValidationException dbEx)
{
ViewBag.ErrorMessage = Common.ErrorCheck.CombineDbErrors(dbEx);
}
catch (Exception ex)
{
ViewBag.ErrorMessage = ErrorCheck.FriendlyError(ex);
}
return View(followupitems);
}
EDIT
The GET issue has been solved thanks to Quinton’s answer below, however I’m still having issues with setting the value. Using the @Html.DropDownList("CategoryID") method returns a null value to the controller everytime (for the Status value, all others are as they should be), and using the @Html.DropDownListFor(m => m.Status, ViewBag.CategoryID) syntax results in the following error message:
‘Compiler Error Message: CS1973: ‘System.Web.Mvc.HtmlHelper’ has no applicable method named ‘DropDownListFor’ but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.’
FINAL EDIT
The following cast in my View allowed me to properly set the values
@Html.DropDownListFor(m => m.Status, (SelectList)ViewBag.CategoryID)
Your item.Status field does contain an
Oor aC? If so then this may work, change theGETmethod to:I don’t know what’s wrong with your
@Html.DropDownList("CategoryID"), you may want to change it to:I use it as i find it to be more expressive – i can cleary see what model property i want it to bind to.
That may sort out your model binding issue. Just a note – before you can set the the Entity’s state, you must first attach it to the db context,