在asp.net mvc的强类型视图(strongly typed view)下拉列表,在Controller中设定的选择项在View上选中失效了。问题就出在DropDownList的Name和ViewData相同了,只要修改一下Name如下就可以了
在asp.net mvc的强类型视图(strongly typed view)下拉列表,在Controller中设定的选择项在View上选中失效了。
下面是我的代码
AllUnSendMail
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AllUnSendMail(FormCollection collection)
{
int sysId = Convert.ToInt32(collection["ddlSysid"]);
int selectedVal =-1;
List<SystemInfoEntity> allSys = SystemInfoClient.GetAll();
allSys.Insert(0, new SystemInfoEntity
{
SysID = -1,
Name = "请选择"
});
if (sysId > 0)
{
selectedVal = sysId;
}
SelectList serviceList = new SelectList(allSys, "SysID", "Name", selectedVal);
ViewData["ddlDataSysid"] = serviceList;
ViewData["Subject"] = string.Empty;
var viewData = MessageQueries.GetAllUnSendMails(sysId, 1, 15);
return View(viewData);
}
相应的视图的下列列表
<%=Html.DropDownList("ddlDataSysid", (SelectList)ViewData["ddlDataSysid"])%>
问题就出在DropDownList的Name和ViewData相同了,只要修改一下Name如下就可以了
<%=Html.DropDownList("ddlSysid", (SelectList)ViewData["ddlDataSysid"])%>