Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict selection to single select of a listboxFor- MVC 2

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

    <%: Html.ListBoxFor(m => m.Subscribers, new List<SelectListItem>(), new { @class = "list_style_Wizard" })%>

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

like image 788
Null Pointer Avatar asked Dec 04 '22 22:12

Null Pointer


1 Answers

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.

like image 149
Glenn Avatar answered Jan 10 '23 15:01

Glenn