保留“提交 Razor 后下拉列表的选定值”



使用提交按钮后,如何将所选值保留在下拉列表中?

@{
    var list = new SelectList(new[]
        {
            new {ID="A", Name="All"},
            new {ID="S", Name="Success"},
            new {ID="P", Name="Pending"},
            new {ID="F", Name="Failed"},
        },
        "ID", "Name", 0);
}
@Html.DropDownList("drpStatus", list, new { htmlAttributes = new { @class = "form-control" } })

抱歉回复晚了。这是我的代码。

查看代码:

@Html.DropDownList("drpStatus", (IEnumerable<SelectListItem>)ViewData["drp_bind"])

控制器代码:

List<SelectListItem> drpStatus = new List<SelectListItem>() {
   new SelectListItem {
       Text = "All", Value = "A" 
   },
   new SelectListItem {
       Text = "Success", Value = "S"
   },
   new SelectListItem {
       Text = "Pending", Value = "P"
   },
   new SelectListItem {
       Text = "Failed", Value = "F"
   },
};
ViewData["drp_bind"] = drpStatus;

这就是我绑定下拉列表的方式。

现在,提交后保留下拉的选定值的主要逻辑:

if (Request.QueryString["Status"] != null)
{
     ViewData["Status"] = HttpUtility.UrlDecode(Convert.ToString(Request.QueryString["Status"]));
     if (ViewData["Status"] != null)
     drpStatus.Where(i => i.Value == ViewData["Status"].ToString()).First().Selected = true;
     ViewData["drp_bind"] = drpStatus;
}

Request.QueryString["Status"] != in this我在下拉列表中保持我的选定值...

相关内容

最新更新