asp.net mvc SelectList 的selected 失效及解决方案

在asp.net mvc的强类型视图(strongly typed view)下拉列表,在Controller中设定的选择项在View上选中失效了。
下面是我的代码

[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"])%>

本文来自云栖社区合作伙伴“doNET跨平台”,了解相关信息可以关注“opendotnet”微信公众号

上一篇:实用的Linux SHELL面试问题及答案


下一篇:jar冲突问题:MVN package 时 SLF4J: Class path contains multiple SLF4J bindings 错误解决办法