Create Dropdown in MVC with static value

Total Post:4

Points:28
C#
 387  View(s)
Ratings:
Rate this:

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.

  1. Post:44

    Points:310
    Re: Create Dropdown in MVC with static value

    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