C# 根据数据类型在MVC 3中自动生成HTML属性
我在MVC模型中具有以下属性:C# 根据数据类型在MVC 3中自动生成HTML属性,c#,.net,asp.net-mvc,asp.net-mvc-3,razor,C#,.net,Asp.net Mvc,Asp.net Mvc 3,Razor,我在MVC模型中具有以下属性: [Range(0, double.MaxValue, ErrorMessage = "The Volume must have positive values!")] public decimal? Volume { get; set; } 生成的HTML是 <input type="text" value="1,00" name="Product.Volume" id="Product_Volume" data-val-range-min=
[Range(0, double.MaxValue, ErrorMessage = "The Volume must have positive values!")]
public decimal? Volume { get; set; }
生成的HTML是
<input type="text" value="1,00" name="Product.Volume" id="Product_Volume" data-val-range-min="0" data-val-range-max="1.79769313486232E+308" data-val-range="The Volume must have positive values!" data-val-number="The field Volume must be a number." data-val="true" class="text-box single-line">
如何使生成的HTML如下所示:
<input type="text" value="1,00" name="Product.Volume" id="Product_Volume" data-val-range-min="0" data-val-range-max="1.79769313486232E+308" data-val-range="The Volume must have positive values!" data-val-number="The field Volume must be a number." data-val="true" class="text-box single-line" data-type="decimal" >
区别在于额外的数据类型=“decimal”
我希望自动添加HTML属性,这样我就不必手动添加它。为
Decimal
类型创建自己的显示模板和编辑器模板视图,通过这种方式,您可以控制它的显示,然后无论何时调用Html.DisplayFor(m=>m.DecimalType)
或Html.EditorFor(m=>m.DecimalType)
将这些视图添加到文件夹视图>共享>显示模板和编辑器模板中
例如,您的EditorTemplate类似于:
@model decimal
@{
Layout = "~/Views/Shared/EditorTemplates/Template.cshtml";
}
@Html.TextBoxFor(x => x, new {data-type = "decimal"})
我的解决方案是重写HTML TextBoxFor helper方法:
public static MvcHtmlString TextBoxWithCustomHtmlAttributesFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
Type propertyType = typeof(TProperty);
Type undelyingNullableType = Nullable.GetUnderlyingType(propertyType);
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
string prefix = ExpressionHelper.GetExpressionText(expression);
var validationAttributes = helper.GetUnobtrusiveValidationAttributes(prefix, metadata);
string pType = (undelyingNullableType ?? propertyType).Name.ToString().ToLower();
if (htmlAttributes != null)
{
var dataTypeDict = new Dictionary<string, object>();
dataTypeDict.Add("data-type", pType);
if (validationAttributes != null && validationAttributes.Count > 0)
{
foreach (var i in validationAttributes)
{
dataTypeDict.Add(i.Key, i.Value);
}
}
htmlAttributes = Combine(new RouteValueDictionary(htmlAttributes), new RouteValueDictionary(dataTypeDict));
}
var textBox = helper.TextBoxFor(expression, (Dictionary<string, object>)htmlAttributes);
return textBox;
}
public static MvcHtmlString TextBoxWithCustomHtmlAttributesFor(此HtmlHelper帮助程序、表达式、对象htmlAttributes)
{
类型propertyType=typeof(tpProperty);
类型undelyingNullableType=Nullable.GetUnderlyingType(propertyType);
var metadata=modelmetada.FromLambdaExpression(表达式,helper.ViewData);
字符串前缀=ExpressionHelper.GetExpressionText(表达式);
var validationAttributes=helper.GetUnobtrusiveValidationAttributes(前缀,元数据);
字符串pType=(undelyingNullableType??propertyType).Name.ToString().ToLower();
如果(htmlAttributes!=null)
{
var dataTypeDict=新字典();
dataTypeDict.Add(“数据类型”,pType);
if(validationAttributes!=null&&validationAttributes.Count>0)
{
foreach(ValidationAttribute中的var i)
{
dataTypeDict.Add(i.Key,i.Value);
}
}
htmlAttributes=Combine(新RouteValueDictionary(htmlAttributes),新RouteValueDictionary(dataTypeDict));
}
var textBox=helper.TextBoxFor(表达式,(字典)htmlAttributes);
返回文本框;
}
谢谢您的回答。然后我是否必须到处创建搜索/替换?另外,我想手动放置的数据注释会发生什么情况?@DragosDurlut没有问题,没有问题,您不必到处搜索/替换,因为您的模型将自动拾取给定类型的模板decimal
。您的其他数据注释应该保持不变