如何从ASP.NET MVC中的枚举中设置dropdownlist中的默认值?

发布于 2025-01-24 16:43:19 字数 1145 浏览 2 评论 0 原文

我有这样的性别枚举: 我的enumlist.cs,

 public enum Gender
{
    [Description("male")]
    male= 0,
    [Description("female")]
    female= 1,
   
}

但是当它显示在视图中时,我需要下拉以显示---请选择---作为默认值。这样我就可以在jQuery脚本中检查所需的验证。

cteate.html,

   <div class="col-xl-4 col-md-6 col-12">
                    <div class="row">
                        <div class="col-xl-4 col-md-4 col-12 form-title">
                            @Html.LabelFor(model => model.Gender) 
                        </div>
                        <div class="col-xl-8 col-md-8 col-12 form-data">
                          

                        @Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(SubMIS.Models.Gender))), "---Please Select ---", new { @class = "rwd-select" })
                        @Html.ValidationMessageFor(model => model.Gender)
                        <label style="color: red">@ViewBag.GenderError</label>


                        </div>
                    </div>
                </div>

该怎么做?我的代码怎么了?

I have a gender enum like this:
my enumlist.cs

 public enum Gender
{
    [Description("male")]
    male= 0,
    [Description("female")]
    female= 1,
   
}

But when it is displayed in View, I need DropDown to display ---Please Select --- as default value. So that i can check required validation in jQuery Script.

cteate.html ,

   <div class="col-xl-4 col-md-6 col-12">
                    <div class="row">
                        <div class="col-xl-4 col-md-4 col-12 form-title">
                            @Html.LabelFor(model => model.Gender) 
                        </div>
                        <div class="col-xl-8 col-md-8 col-12 form-data">
                          

                        @Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(SubMIS.Models.Gender))), "---Please Select ---", new { @class = "rwd-select" })
                        @Html.ValidationMessageFor(model => model.Gender)
                        <label style="color: red">@ViewBag.GenderError</label>


                        </div>
                    </div>
                </div>

How to do this? what's wrong with my code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-01-31 16:43:19

用于从 enum 中显示 asp.net 的值
在助手类中 asp-items =“@html.getEnumSelectList&lt; yourEnumname&gt; 。但是,在代码中,您否则您正在执行此操作,以便您无法使用该格式设置一个格式为设置值,请使用

public enum Gender
    {
        [Description("male")]
        male = 0,
        [Description("female")]
        female = 1,
        
    }

>不

 @using DotNetMVCWebApp.Enums
 <select asp-for="Gender" asp-items="@Html.GetEnumSelectList<Gender>()">
   <option selected="selected" value="1">Please select</option>
</select>

[descript> [descript> naber = 2,[description(“ heapen”)]女性= 3,,以便我可以将默认值设置为1 请参阅下面的默认值:

”在此处输入图像描述”

输出:

< img src =“ https://i.sstatic.net/t5pft.gif” alt =“在此处输入图像说明”>

可以轻松处理

修改枚举:

public enum Gender
    {
        [Display(Name = "Please Select")] Select = 0,
        [Display(Name = "Male")] Male = 1,
        [Display(Name = "Female")] Female = 2,
       
    }

注意:如果您可以在此处查看,我将默认枚举请选择,以便在下拉列表,您无需做任何其他事情。

视图:

@using DotNet6MVCWebApp.Enums
@Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Gender))), new { @class = "rwd-select" })

注意:在这里要记住要记住“ ---请从您的@html.dropdownlistfor中选择---” ,因为这将来自 enum 。只有这将需要最少修改您要实施的内容。

但我更喜欢下面:

@using DotNet6MVCWebApp.Enums
@Html.DropDownList("Gender", Html.GetEnumSelectList<Gender>(),  new { @class = "form-control" })

输出:

“在此处输入图像说明”

For displaying value from Enum in asp.net has built
in helper class asp-items="@Html.GetEnumSelectList<YourEnumName>. However, in your code you are doing otherwise so that you couldn't set a value in that format as setting value using DropDownListFor is not supported. Let's move to the solution.

Enum:

public enum Gender
    {
        [Description("male")]
        male = 0,
        [Description("female")]
        female = 1,
        
    }

View:

 @using DotNetMVCWebApp.Enums
 <select asp-for="Gender" asp-items="@Html.GetEnumSelectList<Gender>()">
   <option selected="selected" value="1">Please select</option>
</select>

Note: I have modified the Enum value as [Description("male")] male = 2, [Description("female")] female = 3, so that I could set default value as 1 as you can see the default value below:

enter image description here

Output:

enter image description here

Work Around To Handle Easily

Modify The Enum:

public enum Gender
    {
        [Display(Name = "Please Select")] Select = 0,
        [Display(Name = "Male")] Male = 1,
        [Display(Name = "Female")] Female = 2,
       
    }

Note: If you can see here I am setting the the default enum as Please Select so that it will be assigned value on DropDownListFor and you do not need to do anything else.

View:

@using DotNet6MVCWebApp.Enums
@Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Gender))), new { @class = "rwd-select" })

Note: Here point to remember just remove "---Please Select ---" from your @Html.DropDownListFor as this will come from the Enum. And only this will required least modification what you are trying to implement.

But I prefer Like below:

@using DotNet6MVCWebApp.Enums
@Html.DropDownList("Gender", Html.GetEnumSelectList<Gender>(),  new { @class = "form-control" })

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文