I am using Html.DropDownList
to create an options list like so
Html.DropDownList("LogType", new SelectList(Model.LogTypeList, "ID", "Name", Model.SelectedLogType),"-- ALL --");
As you can see I pass in a list but also pass in an extra argument to add an additional option: "-- All --"
. The result is as follows:
<select name="LogType" id="LogType">
<option value="">-- ALL -- </option>
<option value="1">Debug</option>
<option value="2" selected="selected">Error</option>
</select>
How do I give -- All --
an value of 0 without having to manually construct the drop down list myself?
The add() method is used to add an option to a drop-down list.
Example Explained HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.
I'm not sure but, why don't you construct the list just before your @html.DropDownList
var myList = new SelectList(Model.LogTypeList, "ID", "Name", Model.SelectedLogType);
and then append your desired item to it.
var myList.add(new selectItem("-- ALL --",0));
and finally pass it to your syntax and see what this will result
Html.DropDownList("LogType",myList);
sorry, this is a quick answer using my iPhone, not sure if it will fix your problem, but I tried to help as much as I could.
I tried to do this before and end with using jQuery:
$("#LogType").append($("<option />").val("0").html("-- All --"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With