public enum ENUM
{
a=1,b,c
}
<%=Html.HiddenFor(model=>model.AEnum)%> //这里总是输出字符串
请问有办法输出Int32吗?
给你个方案参考:重写一个ModelMetadataProvider,具体如下
public class CustomMetadataProvider : DataAnnotationsModelMetadataProvider{ protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) { var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); if (modelType == typeof(ENUM)) { metadata.Model = ((int)((ENUM)metadata.Model)).ToString(); } return metadata; }}
protected void Application_Start(){ AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ModelMetadataProviders.Current = new CustomMetadataProvider();}
经测试,这样至少会影响editfor和hiddenfor,labelfor不受影响。具体原理可以参考一本书:http://www.cnblogs.com/P_Chou/archive/2010/11/01/details-asp-net-mvc-content.html
英文版中第427页开始关于Model Metadata的相关内容
<%=Html.Hidden("AEnum", (int)Model.AEnum)%>