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

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

问题描述

我有我使用一个用户管理页面枚举的列表。我使用MVC中的5.1新的HtmlHelper,让我创造枚举值的下拉列表。我现在有必要从列表中删除挂起值,该值将永远只能编程设置,应该不会被用户设置。

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天全站免登陆