user9187119
user9187119

Reputation:

Using ASP.NET MVC form-group with dropdown list?

How to make an dropdown list in form-group with method="post"? The list has to be dynamic, and the parameter send has to be something else from what is shown in label. I was trying something like this:

    <div class="form-group">
        <label asp-for="Product.Category">Choose category:</label>
        <select class="form-control" id="Product.Category">
@foreach (var category in Model.Categories)
{
            <option>category.CategoryName</option>
}
        </select>
    </div>

But it doesn't work for me, and I have to send to controller the category Id not category name.

The goal is that:

User selects category from dropdown list, and the Id of category is being send to controller.

Upvotes: 1

Views: 1064

Answers (1)

dovid
dovid

Reputation: 6472

<option value="@category.Id">@category.CategoryName</option>

Upvotes: 1

Related Questions