Dragon
Dragon

Reputation: 1088

Specific ListBox value always selected

I am using ListBoxFor where the user can select multiple values ,A user wants that he should get one value always selected for ex:

ListBox contains:
Id   Category
1    A
2    B
3    C
4    D
5    General

here General category should always be selected and user should not be able to change this selected value from ListBox.

@Html.ListBoxFor(model => model.SelectedCategory, ViewBag.ListCategories as MultiSelectList, new { })

I am using Asp.Net MVC 5

Actually this is we can say a very rare scenario,One approach i can use is to use some jquery.If anyone has any solution for this in MVC helpers then plz tell me other wise i will be using jquery.

Upvotes: 0

Views: 234

Answers (1)

Manoj
Manoj

Reputation: 5071

Try this using jquery

  $(document).ready(function () {
       $('#SelectedCategory').find('option[value="5"]').attr('selected','selected');
  });

This works fine.

Upvotes: 1

Related Questions