包括使用ViewBag中的DropDownList绑定“全部”选项 [英] Include 'ALL' option in dropdownlist bind using ViewBag
问题描述
我有Viewbag从控制器绑定鉴于DropDownList中如下:
I have bind the dropdownlist in view by Viewbag from controller as following :
ViewBag.test = from p in _userRegisterViewModel.GetEmpPrimary().ToList().Where(a => a.UserType_id != Convert.ToInt32(Session["loginUserType"].ToString()))
select new
{
Id = p.EmpId,
Name = p.First_Name.Trim() + " " + p.Last_Name.Trim()
};
在视图我有绑定如下:
@Html.DropDownListFor(model => model.EmpId, new SelectList(@ViewBag.test, "Id", "Name"),
new { @class = "form-control", id="ddlEmp" })
现在我想在这个DropDownList中插入ALL和 - 选择 - ..我怎样才能做到这一点..
谁能帮我做到这一点..
在此先感谢..
Now i want to Insert "ALL" and "--Select--" in this dropdownlist.. How can i do this.. Can anyone help me to do this.. Thanks in advance..
推荐答案
您可以通过使用的<一个添加空
选项将DropDownList href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.118).aspx#M:System.Web.Mvc.Html.SelectExtensions.DropDownListFor%3C$c$c%3E2%28System.Web.Mvc.HtmlHelper%7B%3C/$c$c%3E0%7D,System.Linq.Ex$p$pssions.Ex$p$pssion%7BSystem.Func%7B%3C$c$c%3E0,%3C/$c$c%3E1%7D%7D,System.Collections.Generic.IEnumerable%7BSystem.Web.Mvc.SelectListItem%7D,System.String%29\"相对=nofollow>重载 DropDownlistFor()
,接受一个 optionLabel
,例如<的/ p>
You can add a null
option to the dropdownlist by using one of the overloads of DropDownlistFor()
that accepts a optionLabel
, for example
@Html.DropDownListFor(m => m.EmpId, new SelectList(@ViewBag.test, "Id", "Name"), "--select--", new { @class = "form-control", id="ddlEmp" })
这将产生第一个选项为&LT;期权价值=&GT; - 选择 - &LT; /选项&GT;
不过,如果你想包括两个选项 - 选择 -
和全部
你需要生成你自己的的IEnumerable&LT; SelectListItem&GT;
控制器并把它传递给视图。我会建议使用带有的IEnumerable&LT视图模型; SelectListItem&GT;
属性的选项,但使用 ViewBag
中,$ C在控制器$ C将
However, if you want to include options with both "--select--"
and "ALL"
you will need to generate you own IEnumerable<SelectListItem>
in the controller and pass it to the view. I would recommend using view model with a IEnumerable<SelectListItem>
property for the options, but using ViewBag
, the code in the controller would be
List<SelectListItem> options = _userRegisterViewModel.GetEmpPrimary()
.Where(a => a.UserType_id != Convert.ToInt32(Session["loginUserType"].ToString()))
.Select(a => new SelectListItem
{
Value = a.EmpId.ToString(),
Text = a.First_Name.Trim() + " " + a.Last_Name.Trim()
}).ToList();
// add the 'ALL' option
options.Add(new SelectListItem(){ Value = "-1", Text = "ALL" });
ViewBag.test = options;
请注意,我已经给出了所有
选项 1
假设值,没有你的<$ C的$ C> EMPID 值将 1
Note that I have given the ALL
option a value of -1
assuming that none of your EmpId
values will be -1
然后在视图中,您的code产生的DropDownList将
Then in the view, your code to generate the dropdownlist will be
@Html.DropDownListFor(m => m.EmpId, (Ienumerable<SelectListItem>)ViewBag.test, "--select--", new { @class = "form-control" })
不知道为什么你想修改 ID
从 ID =EMPID
属性 ID =ddlEmp
?
然后在POST方法,首先检查的ModelState
是无效的(如果用户选择了 - 选择 -
选项,值空
将被张贴和模式将是无效的),所以返回视图(不要忘了重新分配 ViewBag.test
属性)。
Then in the POST method, first check if ModelState
is invalid (if the user selected the "--select--"
option, a value of null
will be posted and the model will be invalid), so return the view (don't forget to reassign the ViewBag.test
property).
如果的ModelState
是否有效,然后检查 model.EmpId
的价值。如果它的 1
,然后选择用户全部
,不然就选择了特定的选项。
If ModelState
is valid, then check the value of model.EmpId
. If its -1
, then the user selected "ALL"
, otherwise they selected a specific option.
这篇关于包括使用ViewBag中的DropDownList绑定“全部”选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!