0

I want a select HTML element on my page to display the choice of billers for making a payment. In addition to the options for billers I want to display a default option "Select" in the select element. I am using DropDownListFor of Html helper as given below:

@Html.DropDownListFor(m => m.BillerId, new SelectList(Model.Billers,
                   "BillerId", "BillerName", "Select"), new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.BillerId, "", new { @class = "text-danger" })

The HTML output I get is as given below:

<select class="form-control valid" data-val="true" data-val-number="The field BillerId must be a number." id="BillerId" name="BillerId">
    <option value="1">Idea</option>
    <option value="2">Airtel</option>
</select>

The default value Select is not showing up. Please let me know if I am mistaking anything.

2 Answers 2

2

From your question, the ) is in the wrong spot.

What you have:

@Html.DropDownListFor(m => m.BillerId, new SelectList(Model.Billers,
               "BillerId", "BillerName", "Select"), new { @class = "form-control" })

What it needs to be:

@Html.DropDownListFor(m => m.BillerId, new SelectList(Model.Billers,
               "BillerId", "BillerName"),"Select", new { @class = "form-control" })

This will place the "Select" string in the correct position in relation to the overloaded DropDownListFor method.. specifically the optionLabel parameter.

I hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

There is a string parameter after selectList called optionLabel like following.

@Html.DropDownListFor(m => m.BillerId, new SelectList(Model.Billers,
                   "BillerId", "BillerName"), "Select", new { @class = "form-control" })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.