开发者

Restrict selection to single select of a listboxFor- MVC 2

开发者 https://www.devze.com 2023-03-06 02:15 出处:网络
I am using following code to generate a list box.. <%: Html.ListBoxFor(m => m.Subscribers, new List<SelectL开发者_运维百科istItem>(), new { @class = \"list_style_Wizard\" })%>

I am using following code to generate a list box..

    <%: Html.ListBoxFor(m => m.Subscribers, new List<SelectL开发者_运维百科istItem>(), new { @class = "list_style_Wizard" })%>

But we can select more than one items from the listbox.. How can i restric it to single select ???


The HTML helpers DropDownListFor and ListBoxFor seem to add the multiple attribute when rendering as a listbox. I use a combination of the DropDownListFor/ListBoxFor and a jQuery livequery selector to remove the multiple attribute. In Razor use:

@Html.DropDownListFor(m => m.SelectedId, Model.SelectList, 
    new { size = 10, @class = "selectOneListBox" })

and in JavaScript:

$(".selectOneListBox").livequery(function () {
    $(this).removeAttr('multiple');
});

I'm sure you could also write your own version of the HTML helper routine that doesn't spit out the multiple attribute.


Html.ListBoxFor is used to render a multiple choice list box. For single choice use Html.DropDownListFor

<%: Html.DropDownListFor(m => m.Subscribers, listOfsubscribers, new { @class = "list_style_Wizard" }) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消