如何使用MVC在选定的下拉列表中添加默认值 [英] How to add a default value in selected drop down list using MVC
本文介绍了如何使用MVC在选定的下拉列表中添加默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮我在下拉列表中添加默认值。这是填充下拉列表。
提前致谢。下面是我的单行代码。
Please help me to add a default value in drop down list. This is populated drop down list.
Thanks in advance. below is my single line code.
<pre>@Html.DropDownList("leaveCode", ViewBag.leaveCode as List<SelectListItem>,"Select", new { htmlAttributes = new { @class = "form-control" }, @id = "locationsel", @style = "width:150px;" })
在上面的行代码中我想添加一个名为some text的值,该值应显示在下拉列表中。
我尝试过:
请帮帮我。
谢谢
in above line code i want to add a value called "some text" that should be display in drop down list.
What I have tried:
Please help me out .
Thanks
推荐答案
是的。它是leaveCode的默认值。
in viewBag
Yes. It is default value of the leaveCode.
in viewBag
ViewBag.leaveCode = new SelectList(db.ApplyLeaveVMs, "alid", "leaveCode", applyLeaveVM.leaveCode);
以上是我的ViewBang
如果我这样做如下所示,它将通过错误。
above is my ViewBang
if I do like below it will through error.
ViewBag.leaveCode = new SelectList(db.ApplyLeaveVMs, "alid", "leaveCode","LOP", applyLeaveVM.leaveCode);
所以,请告诉我如何在ddl中添加默认值。
So, please let me know how I can add default value in ddl.
这篇关于如何使用MVC在选定的下拉列表中添加默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文