我有一个强类型的 EDIT 视图,该模型具有 2 个字段:名称和类别。名称只是一个字符串,类别是从下拉列表中选择的。
我的控制器:
[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);
}
我的观点:
<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>
现在下拉列表只显示我可以选择的所有选项,但不显示已经选择的选项。我怎样才能让它首先显示已经显示的类别?