I have an EDIT view strongly typed to a model that has 2 fields: Name and category. Name is just a string and category is chosen from a dropdown list.
My controller:
[HttpGet]
public ActionResult EditAuthor(int id)
{
var db = new AuthorDatacontext();
var Author = db.Authors.Find(id);
ViewBag.category = new SelectList(new[] { "ScienceFiction", "fantasy", "LoveStory", "History" });
return View(Author);
}
My View:
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.category)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.category, (SelectList)ViewBag.category)
@Html.ValidationMessageFor(model => model.category)
</div>
Right now the dropDown list just shows all the options that i can choose from but not the option that was already chosen. How i can make it show first the category that was shown already?
Model.category
property on page load is something that's in the drop down list?