Populating Dropdown in ASP.net Core

你说的曾经没有我的故事 提交于 2019-12-05 06:30:42

Looking at the documentation it appears that asp.net core may be moving away from HTML helpers and towards the use of tag helpers. The following link should help

https://docs.asp.net/en/latest/mvc/views/working-with-forms.html#the-select-tag-helper

specifically

@model CountryViewModel

<form asp-controller="Home" asp-action="Index" method="post">
<select asp-for="Country" asp-items="Model.Countries"></select> 
<br /><button type="submit">Register</button>
</form>

Note the use of "asp-for" which references the Model attribute to bind and the use of "asp-items" which references the model attribute source for the List of Select List items and how it is applied to the select tag

The sample model used in the documentation is referenced below for completeness

namespace FormsTagHelper.ViewModels
{
public class CountryViewModel
{
    public string Country { get; set; }

    public List<SelectListItem> Countries { get; } = new List<SelectListItem>
    {
        new SelectListItem { Value = "MX", Text = "Mexico" },
        new SelectListItem { Value = "CA", Text = "Canada" },
        new SelectListItem { Value = "US", Text = "USA"  },
    };
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!