ASP.NET MVC DROPDOWNLIST不渲染的价值选择 [英] ASP.NET MVC dropdownlist not selecting value on render
问题描述
我有这个恼人的问题在我的DROPDOWNLIST不会选择默认的当前值。
控制器:
VAR YearsCycling =新的SelectList(新名单< SelectListItem>()
{
新SelectListItem(){值=1年,文本=1年},
新SelectListItem(){值=1-3yrs,文本=1-3yrs},
新SelectListItem(){值=3-5yrs,文本=3-5yrs},
新SelectListItem(){值=5-10yrs,文本=5-10yrs},
新SelectListItem(){值=10 +年,文本=10 +年}
},
值,
文本,
新SelectListItem(){值=5-10yrs,文本=5-10yrs
选定=真});
ViewBag.YearsCycling = YearsCycling;
查看:
<$p$p><$c$c><%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,\"-select-\") %GT;但不是选择5-10yrs,它只是显示请选择 - 选项,如果我检查DOM源,没有任何元素都被选中。
更新:我仍然不知道是什么问题,但我得到了它的工作,现在通过做一些丑陋的:
&LT;%
VAR SL =(的SelectList)ViewBag.YearsCycling;
%GT;
&LT;选择名称=YearsCyclingID =YearsCycling&GT;
&LT;%的foreach(在SL VAR LI){%GT;
&LT;期权价值=&LT;%:li.Value%&gt;中&LT;%,如果(li.Selected){%GT;选择=真正的&LT;%}%GT;&GT;&LT;%:li.Text%GT;&LT; /选项&GT;
&LT;%}%GT;
&LT; /选择&GT;
这是不是最好的解决办法,但如果你已经到这个问题,因为你一直在拉你的头发,这应该有所帮助。
您code应该的。
VAR YearsCycling =新的List&LT; SelectListItem&GT;()
{
新SelectListItem(){值=1年,文本=1年},
新SelectListItem(){值=1-3yrs,文本=1-3yrs},
新SelectListItem(){值=3-5yrs,文本=3-5yrs},
新SelectListItem(){值=5-10yrs,文本=5-10yrs,选择= TRUE},
新SelectListItem(){值=10 +年,文本=10 +年}
};ViewBag.YearsCycling = YearsCycling;
您可以使用此重载来代替。
&LT;%:Html.DropDownListFor(型号=&GT; model.YearsCycling,ViewBag.YearsCycling为列表&LT; SelectListItem&GT;)%GT;
I have this annoying problem where my DropDownlist doesn't select the current value by default.
Controller:
var YearsCycling = new SelectList(new List<SelectListItem>()
{
new SelectListItem(){ Value="1yr", Text="1yr"},
new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"},
new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"},
new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"},
new SelectListItem(){ Value="10+yrs", Text="10+yrs"}
},
"Value",
"Text",
new SelectListItem() { Value = "5-10yrs", Text = "5-10yrs",
Selected = true });
ViewBag.YearsCycling = YearsCycling;
View:
<%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %>
but instead of selecting "5-10yrs", it just shows the "-select-" option and if I inspect the DOM source, none of the elements are selected.
UPDATE: I still don't know what the problem is, but I got it working for now by doing something ugly:
<%
var sl = (SelectList)ViewBag.YearsCycling;
%>
<select name="YearsCycling" id="YearsCycling">
<%foreach(var li in sl){ %>
<option value="<%:li.Value %>" <%if(li.Selected){%>selected="true"<%}%>><%:li.Text %></option>
<%} %>
</select>
This isn't the best solution, but if you've come to this question because you've been pulling your hair out, this should help.
Your code should be.
var YearsCycling = new List<SelectListItem>()
{
new SelectListItem(){ Value="1yr", Text="1yr"},
new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"},
new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"},
new SelectListItem(){ Value="5-10yrs", Text="5-10yrs", Selected=true},
new SelectListItem(){ Value="10+yrs", Text="10+yrs"}
};
ViewBag.YearsCycling = YearsCycling;
You could use this overload instead.
<%:Html.DropDownListFor(model=>model.YearsCycling,ViewBag.YearsCycling as List<SelectListItem>) %>
这篇关于ASP.NET MVC DROPDOWNLIST不渲染的价值选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!