我创建了一个下拉列表,可以很好地传递该数据。但是,当我想编辑表单时,GET 操作中的旧值不会填充到表单中。我是 MVC 的新手,所以一个简洁的例子会很棒。更新 - 我想我知道发生了什么,需要帮助解决。ddl 仅包含我的下拉数据,因此我仅将下拉数据传递给编辑视图中的模型。表单的数据在我的代码下面的变量模型中,如何在视图中传递 ddl 和模型?
这是我在控制器中的代码
public ActionResult Edit(int id)
{
var ddl = new Users();
ddl.DropDowns = userRepository.Getddl("Departments").Select(c => new SelectListItem
{
Value = c.DropdownID.ToString(),
Text = c.DropdownText
});
var model = userRepository.GetUserId(id);
return View(ddl);
}
[HttpPost]
public ActionResult Edit(Models.Users users)
{
userRepository.UpdateUser(users);
return RedirectToAction("Index");
}
看法
@model Models.Users
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div id="stylized" class="myform">
<fieldset>
<legend>User Account</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserFullName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserFullName)
@Html.ValidationMessageFor(model => model.UserFullName)
</div>
<br/>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<br/>
<div class="editor-label">
@Html.LabelFor(model => model.ProfileName)
</div>
<div class="editor-field">
@Html.DropDownListFor(x=> x.DeptID,new SelectList(Model.DropDowns,"Value","Text"),"Select")
</div>