c# - MVC 4: How to make DropDownListFor return 0 as optionLabel value? -


i have create view multiple dropdownlistfors. each time new object created 1 of dropdownlistfors should have value, want others return 0 result when optionlabel left selected.

how assign 0 value dropdownlistfor's optionlabel?

edit: here example of dropdownlistfor code in view:

@html.dropdownlistfor(model => model.cardreward.id, new selectlist(viewbag.cardreward, "id","name"), "none") 

when render page creates list none @ top this:

<option value>none</option> 

i want this:

<option value="0">none</option> 

in documentation dropdownfor optionlabel parameter (where you're passing "none") described as:

the text default empty item.

so designed empty item. need add additional item select list in order 0 value.

i have used following extension method accomplish (sorry untested, there may minor errors):

public ienumerable<selectlistitem> insertemptyfirst(this ienumerable<selectlistitem> list, string emptytext = "", string emptyvalue = "") {     return new [] { new selectlistitem { text = emptytext, value = emptyvalue } }.concat(list); } 

you use this:

@html.dropdownlistfor(model => model.cardreward.id, new selectlist(viewbag.cardreward, "id","name").insertemptyfirst("none", "0")) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -