Create Dropdown in MVC with static value

Hi Expert,

I want to change way of create dropdown menu in my razor view.

Here is my code:

<div class="col-md-6">

                                @{
        List<SelectListItem> listItems = new List<SelectListItem>();
        listItems.Add(new SelectListItem { Text = "Approved", Value = "A" });
        listItems.Add(new SelectListItem { Text = "Pending", Value = "P", Selected = true });
        listItems.Add(new SelectListItem { Text = "Rejected", Value = "R" });
                                }
                                <label for="status">Status</label>
                                @Html.DropDownListFor(model => model.Status, listItems, "-- Select Status --", new { @class = "form-control" })
                            </div>

Please give me some solution to achieve this task.

Last updated:12/14/2017 11:24:11 PM

1 Answers

Harsh kumar srivastava
Harsh kumar srivastava

Hi Aarav,

            I analyzed your code. You can also create static dropdown in your razor view with different manner.

Please follow below line of code.

<label for='status'>Status</label>

                                @Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem { Text = 'Approved', Value = 'A'}, new SelectListItem { Text = 'Pending',
Value = 'P', Selected = true }, new SelectListItem { Text = 'Rejected', Value = 'R' } }, '-- Select Status --', new { @class = 'form-control' })

Hope its helpful!!!!

Answer