【发布时间】:2013-05-08 12:42:18
【问题描述】:
我有一个 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>
现在下拉列表只显示我可以选择的所有选项,但不显示已经选择的选项。 我怎样才能让它首先显示已经显示的类别?
【问题讨论】:
-
您确定页面加载时
Model.category属性中的内容是下拉列表中的内容吗? -
是的,因为我可以在详细信息页面中看到它,我从创建页面的下拉列表中选择它
标签: c# asp.net-mvc asp.net-mvc-4 razor html-helper