Bob*_*ock 4 enums model asp.net-mvc-3
在我的MVC3应用程序中.我正在使用选择列表来填充具有这样的枚举值的组合框
<div class="editor-field">
@Html.DropDownListFor(x => x.AdType, new SelectList(Enum.GetValues(typeof(MyDomain.Property.AdTypeEnum))), " ", new { @class = "dxeButtonEdit_Glass" })
</div>
Run Code Online (Sandbox Code Playgroud)
MyDomain.Property看起来像这样
public enum AdTypeEnum
{
Sale = 1,
Rent = 2,
SaleOrRent = 3
};
Run Code Online (Sandbox Code Playgroud)
如何使用我的本地化字符串来本地化这些枚举?
Dar*_*rov 13
您可以编写自定义属性:
public class LocalizedNameAttribute: Attribute
{
private readonly Type _resourceType;
private readonly string _resourceKey;
public LocalizedNameAttribute(string resourceKey, Type resourceType)
{
_resourceType = resourceType;
_resourceKey = resourceKey;
DisplayName = (string)_resourceType
.GetProperty(_resourceKey, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
.GetValue(null, null);
}
public string DisplayName { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
和自定义DropDownListForEnum助手:
public static class DropDownListExtensions
{
public static IHtmlString DropDownListForEnum<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
string optionLabel,
object htmlAttributes
)
{
if (!typeof(TProperty).IsEnum)
{
throw new Exception("This helper can be used only with enum types");
}
var enumType = typeof(TProperty);
var fields = enumType.GetFields(
BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public
);
var values = Enum.GetValues(enumType).OfType<TProperty>();
var items =
from value in values
from field in fields
let descriptionAttribute = field
.GetCustomAttributes(
typeof(LocalizedNameAttribute), true
)
.OfType<LocalizedNameAttribute>()
.FirstOrDefault()
let displayName = (descriptionAttribute != null)
? descriptionAttribute.DisplayName
: value.ToString()
where value.ToString() == field.Name
select new { Id = value, Name = displayName };
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var enumObj = metadata;
var selectList = new SelectList(items, "Id", "Name", metadata.Model);
return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
}
}
Run Code Online (Sandbox Code Playgroud)
然后很容易:
模型:
public enum AdTypeEnum
{
[LocalizedName("Sale", typeof(Messages))]
Sale = 1,
[LocalizedName("Rent", typeof(Messages))]
Rent = 2,
[LocalizedName("SaleOrRent", typeof(Messages))]
SaleOrRent = 3
}
public class MyViewModel
{
public AdTypeEnum AdType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
AdType = AdTypeEnum.SaleOrRent
});
}
}
Run Code Online (Sandbox Code Playgroud)
视图:
@model MyViewModel
@Html.DropDownListForEnum(
x => x.AdType,
" ",
new { @class = "foo" }
)
Run Code Online (Sandbox Code Playgroud)
最后,你应该创建一个Messages.resx将包含必需的密钥(文件Sale,Rent和SaleOrRent).如果您想要本地化,只需Messages.xx-XX.resx使用相同的键定义一个不同的文化,并交换当前的线程文化.
| 归档时间: |
|
| 查看次数: |
1614 次 |
| 最近记录: |