Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to select true particular item of @Html.ListBoxFor control.
I know for selecting true all items in the @Html.ListBoxFor control-
JavaScript
$("#List").find("option").attr("selected", true);


Please suggest how to select particular item in @Html.ListBoxFor control using JQUERY.

Please suggest.
Thanks.
Posted

1 solution

Method 1:

C#
@Html.ListBox(m => m.Names, (IEnumerable<SelectListItem>)(new[] { new SelectListItem() { Value = "1",Text="Abc",Selected=true }}))

Method 2:

C#
@Html.ListBoxFor(m => m.Names, new SelectList(new listName_With_Id()(), "Id", "Name", "9"), new { Class = "ControlName"})


Here Id with 9 will be selected.
 
Share this answer
 
Comments
Member 8090436 2-Dec-15 9:04am    
Thanks....i am looking this with JQUERY not with control.
Sums Mohs Eds 2-Dec-15 9:16am    
Oh am so sorry, with jquery suppose the structure is
<select multiple="true">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>

then it can be selected like below
$("select option[value=2]").attr("selected", true);

incase if it is multiple selection like i have mentioned it in html code, you can repeat the values as shown below.
$("select option[value=2]").attr("selected", true);
$("select option[value=4]").attr("selected", true);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900