从 MVC 5.1 EnumDropDownListFor 中排除/删除值 [英] Exclude/Remove Value from MVC 5.1 EnumDropDownListFor

查看:19
本文介绍了从 MVC 5.1 EnumDropDownListFor 中排除/删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于用户管理页面的枚举列表.我在 MVC 5.1 中使用新的 HtmlHelper,它允许我为枚举值创建下拉列表.我现在需要从列表中删除 Pending 值,这个值只能以编程方式设置,永远不应由用户设置.

I have a list of enums that I am using for a user management page. I'm using the new HtmlHelper in MVC 5.1 that allows me to create a dropdown list for Enum values. I now have a need to remove the Pending value from the list, this value will only ever be set programatically and should never be set by the user.

枚举:

public enum UserStatus
{
    Pending = 0,
    Limited = 1,
    Active = 2
}

查看:

@Html.EnumDropDownListFor(model => model.Status)

无论如何,是否可以覆盖当前控件,或者编写一个自定义 HtmlHelper 以允许我指定一个枚举或要从结果列表中排除的枚举?或者你会建议我在客户端用 jQuery 做一些事情,以便在下拉列表生成后从下拉列表中删除它?

Is there anyway, either overriding the current control, or writing a custom HtmlHelper that would allow me to specify an enum, or enums to exclude from the resulting list? Or would you suggest I do something client side with jQuery to remove the value from the dropdown list once it has been generated?

谢谢!

推荐答案

您可以构建一个下拉列表:

You could construct a drop down list:

@{ // you can put the following in a back-end method and pass through ViewBag
   var selectList = Enum.GetValues(typeof(UserStatus))
                        .Cast<UserStatus>()
                        .Where(e => e != UserStatus.Pending)
                        .Select(e => new SelectListItem 
                            { 
                                Value = ((int)e).ToString(),
                                Text = e.ToString()
                            });
}
@Html.DropDownListFor(m => m.Status, selectList)

这篇关于从 MVC 5.1 EnumDropDownListFor 中排除/删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆