[英]How to get mvc dropdown selected value
我正在一个MVC项目中,在其中对Employee
对象执行CRUD(创建/读取/更新/删除)操作。
这是问题所在:我正在使用一个下拉列表将DepartmentName
分配给该员工,并且在编辑该员工时,我想在较早分配给他的Department下拉列表中获取该员工的选定值。
这是代码:
主视图
<table class="table" id="Grid">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.GenderName)
</th>
<th>
@Html.DisplayNameFor(model => model.DepartmentName)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.GenderName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepartmentName)
</td>
@if (Context.User.IsInRole("Admin"))
{
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id })
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { onclick = "return confirm('Are sure wants to delete?');" })
</td>
}
</tr>
}
DBHelper
public List<Emloyee> GetEmployees()
{
connection();
List<Emloyee> employeelist = new List<Emloyee>();
SqlCommand cmd = new SqlCommand("GetEmployeeDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
sd.Fill(dt);
con.Close();
foreach (DataRow dr in dt.Rows)
{
employeelist.Add(
new Emloyee
{
Id = Convert.ToInt32(dr["Id"]),
Name = Convert.ToString(dr["Name"]),
GenderName = Convert.ToString(dr["GenderName"]),
DepartmentName = Convert.ToString(dr["DepartmentName"])
});
}
return employeelist;
}
调节器
public ActionResult Edit(int id)
{
EmployeeDB edb = new EmployeeDB();
var depart = edb.GetDepartment();
ViewBag.DepartmentId = new SelectList(depart, "DepartmentId", "DepartmentName");
return PartialView(edb.GetEmployees().Find(cmodel => cmodel.Id == id));
}
编辑视图
<div class="form-group">
@Html.LabelFor(model => model.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DepartmentId",(SelectList)ViewBag.DepartmentId, new { @class = "form-control" })
</div>
</div>
如之前在注释中所述,您的Employee模型不包含departmentId
,在模型中包括departmentId
。 我怀疑这是您无法在“编辑视图”上查看部门的原因。 我相信下面的代码片段将解决此问题。
public List<Emloyee> GetEmployees()
{
connection();
List<Emloyee> employeelist = new List<Emloyee>();
SqlCommand cmd = new SqlCommand("GetEmployeeDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
sd.Fill(dt);
con.Close();
foreach (DataRow dr in dt.Rows)
{
employeelist.Add(
new Emloyee
{
Id = Convert.ToInt32(dr["Id"]),
Name = Convert.ToString(dr["Name"]),
GenderName = Convert.ToString(dr["GenderName"]),
DepartmentName = Convert.ToString(dr["DepartmentName"])
DepartmentId = Convert.ToInt32(dr["DepartmentId "])
});
}
return employeelist;
}
控制器动作
public ActionResult Edit(int id)
{
EmployeeDB edb = new EmployeeDB();
var depart = edb.GetDepartment();
ViewBag.Departments = new SelectList(depart, "DepartmentId",
"DepartmentName");
return PartialView(edb.GetEmployees().Find(cmodel => cmodel.Id == id));
}
查看:
<div class="form-group">
@Html.LabelFor(model => model.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DepartmentId",(SelectList)ViewBag.Departments, new { @class = "form-control" })
</div>
</div>
您可以将构造函数设置为在selectList
的默认值时覆盖第四个参数。
ViewBag.DepartmentId = new SelectList(depart, "DepartmentId", "DepartmentName",[setYourDefaultValue]);
编辑
您的DropDownList
需要将SelectListItem
设置为null
@Html.DropDownList("DepartmentId",null, new { @class = "form-control" })
您可以在asp.net MVC selectList
上看到源代码
如果使用此覆盖方法。
DropDownList(HtmlHelper, String, IEnumerable<SelectListItem>, IDictionary<String, Object>)
您需要将selectList
设置为null以获得默认值。
private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, ModelMetadata metadata,
string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool allowMultiple,
IDictionary<string, object> htmlAttributes)
{
string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
if (String.IsNullOrEmpty(fullName))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
}
bool usedViewData = false;
// If we got a null selectList, try to use ViewData to get the list of items.
if (selectList == null)
{
selectList = htmlHelper.GetSelectData(name);
usedViewData = true;
}
....etc
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.