如何在编辑记录时在dropdownList中设置selecteditem?请帮帮我!
以下是控制器:
public ActionResult Edit(int id)
{
ViewModel model = GetViewModel(id);
return View(model);
}
以下是观点:
@model ViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit/Copy Request</h2>
<h3></h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Requests</legend>
<div class="float-left">
@Html.LabelFor(model => model.Crop)
@Html.DropDownList("CropList", string.Empty)
@Html.ValidationMessageFor(model => model.Crop)
</div>
<div class="float-right">
@Html.LabelFor(model => model.Stage)
@Html.EditorFor(model => model.Stage)
@Html.ValidationMessageFor(model => model.Stage)
</div>
</fieldset>
<p>
<input type="submit" value="Save Change" />
</p>
}
&#34; CropList&#34;是一个selectList,它是ViewModel的一部分 @ Html.DropDownList(&#34; CropList&#34;,string.Empty)将获得下拉框的选择列表。但是,如何设置下拉框的selectedItem?使用文本框很容易: @ Html.EditorFor(model =&gt; model.Crop);
休
答案 0 :(得分:1)
将其放在下拉列表中。帮助者将项目绑定到模型
@Html.DropDownListFor(x => x.CropList, SelectList)