In my controller I have the following (code omitted for brevity):
public ActionResult Edit(string id)
{
vw_user u = _respository.getProfile(id);
var model = new UserProfileEditViewModel
{
SL_collegeid = new SelectList(_respository.getColleges(u.State), "id", "college1", u.id)
};
return View(model);
}
Now when I debug the model right before the return it is showing that the particular college (u.id) is selected in the SelectList (SL_collegeid). However, when the page loads the selected item is not selected.
Here is the code for cshtml:
@Html.DropDownListFor(model=>model.SL_collegeid, Model.SL_collegeid)
Could some one please help me out with this?
Thank you in advance,
Chris
Did the view in question have nothing selected when the controller method got called? If so, the model doesn’t overwrite what is in ModelState dictionary– so even if you change the model property that is the backing field for the view, the originally posted data will trump it. Again, if that’s the case, you can get around it by not using the html helpers and manually setting the values in HTML using Razor syntax.