Reputation: 2819
Afternoon all.
I have a drop down list in my view
<div class="editor-label">
@Html.LabelFor(model => model.SiteTypeId, "SiteType")
</div>
<div class="editor-field">
@Html.DropDownList("SiteTypeId", (IEnumerable<SelectListItem>) ViewBag.SiteTypeId)
@Html.ValidationMessageFor(model => model.SiteTypeId)
</div>
With the model looking like so:
public class SiteType
{
[Key]
public int SiteTypeId { get; set; }
public string SiteTypeName { get; set; }
}
Currently there are just the two items in the drop down:
1 (value) - Foo (item)
2 (value) - Bar (item)
Which appears via the controller a la:
public ActionResult Details(Guid id)
{
var model = Model(id);
ViewBag.SiteTypeId = new SelectList(db.SiteTypes, "SiteTypeId", "SiteTypeName");
return View(model.Details);
}
Users will update this accordingly and it will get stored in a database.
I can get the value of the drop down list from the database no problem, but how would I got about effectively binding this value to the list?
Man I feel simple asking this :'(
Any help always appreciated.
Upvotes: 0
Views: 81
Reputation: 1038820
Don't use the same name for the ViewBag
parameter containing the SelectList as the first argument of the DropDownList helper:
ViewBag.SiteTypes = new SelectList(db.SiteTypes, "SiteTypeId", "SiteTypeName");
and in the view:
@Html.DropDownList("SiteTypeId", (IEnumerable<SelectListItem>)ViewBag.SiteTypes)
Now inside the POST action when this form is submitted the selected value will be passed as a parameter called SiteTypeId
.
Upvotes: 1