MVC Html.ListBox-可能的错误? [英] MVC Html.ListBox - Possible Bug?
问题描述
我只是好奇这是否是一个错误,但是当将Html.ListBox
和一起使用时,设置选择的值和设置HtmlAttributes
时,默认选择的值不是已选择.
示例:
以下方法确实有效(默认选择的值1已成功设置):controller.cs
I am just curious if this is a bug, but when using the Html.ListBox
with a combination of setting the selected value and setting the HtmlAttributes
, the default selected value isn''t selected.
Examples:
The following does work (the default selected value of 1 is set successfully):controller.cs
_dt = _ds.Tables["REGION"];
_dv = new DataView(_dt);
_query = from DataRowView rowView in _dv
select rowView;
ViewData["REGIONID"] = new MultiSelectList(_query, "REGIONID", "REGION", new int[] { 1 })
我的aspx页面
<%= Html.ListBox("REGIONID")%>
以下无效不起作用(未设置默认选择的值1):
controller.cs
The following does not work (the default selected value of 1 is NOT set):
controller.cs
_dt = _ds.Tables["REGION"];
_dv = new DataView(_dt);
_query = from DataRowView rowView in _dv
select rowView;
ViewData["REGIONID"] = new MultiSelectList(_query, "REGIONID", "REGION", new int[] { 1 })
我的aspx页面
<%= Html.ListBox("REGIONID", (MultiSelectList)ViewData["REGIONID"],
new { size = 3, onchange = "setCriteria(this.form.REGIONID,this.form.REGIONTEXT)" })%>
任何帮助将不胜感激.
Any help would be Much Appreciated.
推荐答案
当我们运行MVC应用程序时,以下查看"代码
Hi,
When we run the MVC Application the following "View" code
<%:
Html.ListBox("REGIONID", ViewData("REGIONID")), New With { Key .size = 3, Key .onchange = "dispVal()",Key .selected = true})
%>
将转换为此html格式[在浏览器中]
will converted to this html format [in the browser]
<select id=""REGIONID"" name=""REGIONID"" önchange=""dispVal()"" selected=""true"" size=""3"><option" value=""1">1</option><br" mode="hold" /><option value="2">2</option>
<option value="3">3</option>
如果我们想在HTML中为ListBox选择一个特定的值.我们要更改以下格式的代码
if we want to select a particular value in the HTML for ListBox. We want to change the code in the following format
<select id="REGIONID" name="REGIONID" önchange="dispVal" size="3">
<option value="1" selected="true">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
选项属性中的 selected ="true"
但是我不确定是否可以通过MVC.So,因此最好在"body onLoad"中使用JavaScript.
或在正文"结尾处
例如:
selected = "true" in the option property
But I am not sure about that it is possible through MVC.So it is better to use a javascript in the "body onLoad"
Or at the end of "Body"
eg:
<body>
<%
Html.BeginForm()
If True Then
%>
<div>
<%:
Html.ListBox("REGIONID", DirectCast(ViewData("REGIONID"), MultiSelectList), New With { _
Key .size = 3, _
Key .onchange = "dispVal()", _
Key .selected = "true" _
})
%>
<%: Html.TextBox("Id", ViewData("txtVal"))%>
</div>
<% End If%>
<script language="javascript" type="text/javascript">
setVal()
</script>
</body>
<script language="javascript" type="text/javascript">
function dispVal() {
var v = document.getElementById("REGIONID");
alert(v.value);
}
function setVal() {
var v = document.getElementById("REGIONID");
v.selectedIndex = 0;
}
</script>
这篇关于MVC Html.ListBox-可能的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!