TBA
TBA

Reputation: 1197

Razor DropDownFor default text and value

I have created a dropdown list in razor as,

@Html.DropDownListFor(model => model.ItemID, new SelectList(Model.ItemList, "Id", "ItemCodes", String.Empty), "ALL", new { @id = "ItemDetails"})

Here I wanted a default value which I added as 'All'. The text 'All' will be the first element, which is fine. However how can I make sure that the All has the value as '0'.

That is current Html markup is <option value="">ALL</option>. I needed that as <option value="0">ALL</option>.

How can I add that as part of above Razor declaration.

Upvotes: 0

Views: 611

Answers (2)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

Use Jquery to do this:

 $("#ItemDetails").prepend("<option selected=selected value='0'>All</option>");

and ur htmlhelper will be as:

@Html.DropDownListFor(model => model.ItemID, new SelectList(Model.ItemList, "Id", "ItemCodes", String.Empty), new { @id = "ItemDetails"})

Upvotes: 0

Pankaj Sharma
Pankaj Sharma

Reputation: 254

You can not do this by helper syntax instead of this You canto insert an item at 0 index like this.

Model.ItemList.Insert(0,new SelectListItem{Value="0",Text="ALL"});

Upvotes: 3

Related Questions