mileyH
mileyH

Reputation: 176

Create drop down inside Mvc table

I have created mvc5 project with table which I can update locally, one of the field in the table should be drop down list with 2 fixed values like male and female (gender field) ,how should I add it to the table?

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>

Upvotes: 0

Views: 2504

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

You could use the DropDownListFor helper:

@for (var i = 0; i < Model.Values; i++)
{
    <tr>
        <td>
            @Html.DropDownListFor(x => x.Values[i].Gender, Model.Genders)
        </td>
        ...
    </tr>
}

Obviously you should accommodate in your view model the Genders property:

public IEnumerable<SelectListItem> Genders
{
    get
    {
        return new[]
        {
            new SelectListItem { Value = "F", Text = "Female" },
            new SelectListItem { Value = "M", Text = "Male" },
        }
    }
}

Upvotes: 2

Related Questions