asp.core page razor enum dropdownlist

偶尔善良 提交于 2021-02-07 19:53:24

问题


I use Asp.Core 2.1 with "Page Razor Crud" and I want into my page show a dropdownlist.

Here's my model:

public enum EnumStatus
    {
        Waiting,
        [Display(Name = "Development progress")]
        Progress,
        Canceled,
        Refused,
        Deployed,
    }
    public class Improvement
    {
        [Key]
        public int ID { get; set; }

        public string Title { get; set; }

        [DataType(DataType.Text)]
        public string Description { get; set; }

        public EnumStatus Status { get; set; }
    }

the CRUD Razor Page generated this line:

<select asp-for="Improvement.Status" class="form-control"></select>

But it's empty. After researchs, i found I need to add asp-items. But at this point, all solutions I tested failed.

Best regards,


回答1:


Try the following code in the view:

<select asp-for="Status" 
        asp-items="Html.GetEnumSelectList<EnumStatus>()" 
        class="form-control>
    <option selected="selected" value="">Please select</option>
</select>

Here Status is the property of your Improvement model class while EnumStatus is the name of your enum type.



来源:https://stackoverflow.com/questions/53741589/asp-core-page-razor-enum-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!