MVC3 Razor DropDownListFor Enums [英] MVC3 Razor DropDownListFor Enums
问题描述
试图将我的项目更新到 MVC3,但我找不到:
我有一个简单的 ENUMS 数据类型:
公共枚举状态(){AL,AK,AZ,...WY}
在包含此数据类型的模型视图中,我想将其用作 DropDown/SelectList:
公共类 FormModel(){公共状态 状态 {get;放;}}
非常直接:当我为此分部类使用自动生成视图时,它会忽略这种类型.
我需要一个简单的选择列表,当我通过我的 AJAX - JSON POST 方法点击提交和处理时,该列表将枚举的值设置为所选项目.
而不是视图(???!):
@Html.DropDownListFor(model => model.State, model => model.States)
提前感谢您的建议!
我刚刚为我自己的项目制作了一个.下面的代码是我的助手类的一部分,我希望我得到了所有需要的方法.如果不行,请写评论,我会再次检查.
公共静态类 SelectExtensions{public static string GetInputName(Expression表达式){if (expression.Body.NodeType == ExpressionType.Call){MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;字符串名称 = GetInputName(methodCallExpression);return name.Substring(expression.Parameters[0].Name.Length + 1);}return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);}私有静态字符串 GetInputName(MethodCallExpression 表达式){//p =>p.Foo.Bar().Baz.ToString() =>p.Foo 或扔...MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;if (methodCallExpression != null){返回 GetInputName(methodCallExpression);}返回表达式.Object.ToString();}public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) 其中 TModel : class{string inputName = GetInputName(表达式);var 值 = htmlHelper.ViewData.Model == null?默认(TProperty): expression.Compile()(htmlHelper.ViewData.Model);返回 htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()));}public static SelectList ToSelectList(Type enumType, string selectedItem){列表items = new List();foreach (Enum.GetValues(enumType) 中的 var 项目){FieldInfo fi = enumType.GetField(item.ToString());var 属性 = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();var 标题 = 属性 == null ?item.ToString() : ((DescriptionAttribute)attribute).Description;var listItem = 新的 SelectListItem{值 = ((int)item).ToString(),文字 = 标题,Selected = selectedItem == ((int)item).ToString()};items.Add(listItem);}return new SelectList(items, "Value", "Text", selectedItem);}}
将其用作:
Html.EnumDropDownListFor(m => m.YourEnum);
更新
我已经创建了替代的 Html Helpers.要使用它们,您只需在 viewsweb.config
中更改您的 baseviewpage.
有了它们,您就可以做到:
@Html2.DropDownFor(m => m.YourEnum);@Html2.CheckboxesFor(m => m.YourEnum);@Html2.RadioButtonsFor(m => m.YourEnum);
更多信息在这里:http:///blog.gauffin.org/2011/10/first-draft-of-my-alternative-html-helpers/
Trying to get my project updated to MVC3, something I just can't find:
I have a simple datatype of ENUMS:
public enum States()
{
AL,AK,AZ,...WY
}
Which I want to use as a DropDown/SelectList in my view of a model that contains this datatype:
public class FormModel()
{
public States State {get; set;}
}
Pretty straight forward: when I go to use the auto-generate view for this partial class, it ignores this type.
I need a simple select list that sets the value of the enum as the selected item when I hit submit and process via my AJAX - JSON POST Method.
And than the view (???!):
<div class="editor-field">
@Html.DropDownListFor(model => model.State, model => model.States)
</div>
thanks in advance for the advice!
I've just made one for my own project. The code below is part of my helper class, I hope that I got all methods needed. Write a comment if it doesn't work, and I'll check again.
public static class SelectExtensions
{
public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
if (expression.Body.NodeType == ExpressionType.Call)
{
MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
string name = GetInputName(methodCallExpression);
return name.Substring(expression.Parameters[0].Name.Length + 1);
}
return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}
private static string GetInputName(MethodCallExpression expression)
{
// p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
if (methodCallExpression != null)
{
return GetInputName(methodCallExpression);
}
return expression.Object.ToString();
}
public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
string inputName = GetInputName(expression);
var value = htmlHelper.ViewData.Model == null
? default(TProperty)
: expression.Compile()(htmlHelper.ViewData.Model);
return htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()));
}
public static SelectList ToSelectList(Type enumType, string selectedItem)
{
List<SelectListItem> items = new List<SelectListItem>();
foreach (var item in Enum.GetValues(enumType))
{
FieldInfo fi = enumType.GetField(item.ToString());
var attribute = fi.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault();
var title = attribute == null ? item.ToString() : ((DescriptionAttribute)attribute).Description;
var listItem = new SelectListItem
{
Value = ((int)item).ToString(),
Text = title,
Selected = selectedItem == ((int)item).ToString()
};
items.Add(listItem);
}
return new SelectList(items, "Value", "Text", selectedItem);
}
}
Use it as:
Html.EnumDropDownListFor(m => m.YourEnum);
Update
I've created alternative Html Helpers. All you need to do to use them is to change your baseviewpage in viewsweb.config
.
With them you can just do:
@Html2.DropDownFor(m => m.YourEnum);
@Html2.CheckboxesFor(m => m.YourEnum);
@Html2.RadioButtonsFor(m => m.YourEnum);
More info here: http://blog.gauffin.org/2011/10/first-draft-of-my-alternative-html-helpers/
这篇关于MVC3 Razor DropDownListFor Enums的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!