Asp.net mvc 使用模型描述中的title属性为MVC帮助器创建复选框
我创建了一个文本框帮助器,用于添加从模型中字段的“描述”属性中获取的标题(工具提示):Asp.net mvc 使用模型描述中的title属性为MVC帮助器创建复选框,asp.net-mvc,html-helper,Asp.net Mvc,Html Helper,我创建了一个文本框帮助器,用于添加从模型中字段的“描述”属性中获取的标题(工具提示): public static MvcHtmlString TextBoxForWithTitle<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null)
public static MvcHtmlString TextBoxForWithTitle<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string textboxText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();
if (string.IsNullOrEmpty(textboxText))
return MvcHtmlString.Empty;
var textbox = new TagBuilder("input");
textbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
if (!string.IsNullOrEmpty(metaData.Description))
textbox.Attributes.Add("title", metaData.Description);
return MvcHtmlString.Create(textbox.ToString());
}
public static MvcHtmlString TextBoxForWithTitle(此HtmlHelper HtmlHelper,表达式表达式,对象htmlAttributes=null)
{
var metaData=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
字符串htmlFieldName=ExpressionHelper.GetExpressionText(表达式);
string textboxText=metaData.DisplayName??metaData.PropertyName??htmlFieldName.Split('.').Last();
if(string.IsNullOrEmpty(textboxText))
返回MvcHtmlString.Empty;
var textbox=新标记生成器(“输入”);
合并属性(新的RouteValueDictionary(htmlAttributes));
如果(!string.IsNullOrEmpty(metaData.Description))
textbox.Attributes.Add(“title”,metaData.Description);
返回MvcHtmlString.Create(textbox.ToString());
}
我知道复选框也是一个“输入”类型的元素,但我不知道如何构造一个助手来使用描述作为标题
public static MvcHtmlString CheckBoxForWithTitle<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string chkboxText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();
MemberExpression memberExpression = expression.Body as MemberExpression;
string parameterName = memberExpression.Member.Name;
if (string.IsNullOrEmpty(chkboxText))
return MvcHtmlString.Empty;
var chkbox = new MvcHtmlString(
string.Format(
"<input type=\"checkbox\" name=\"{0}\" id=\"{0}\" value=\"{1}\" {2} />",
parameterName,
chkbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
if (!string.IsNullOrEmpty(metaData.Description))
chkbox.Attributes.Add("title", metaData.Description);
return MvcHtmlString.Create(chkbox.ToString());
}
public static MvcHtmlString CheckBoxForWithTitle(此HtmlHelper HtmlHelper,表达式表达式,对象htmlAttributes=null)
{
var metaData=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
字符串htmlFieldName=ExpressionHelper.GetExpressionText(表达式);
字符串chkboxText=metaData.DisplayName??metaData.PropertyName??htmlFieldName.Split('.').Last();
MemberExpression MemberExpression=expression.Body作为MemberExpression;
字符串参数Name=memberExpression.Member.Name;
if(string.IsNullOrEmpty(chkboxText))
返回MvcHtmlString.Empty;
var chkbox=new MvcHtmlString(
字符串格式(
"",
参数名,
合并属性(新的RouteValueDictionary(HtmlatAttributes));
如果(!string.IsNullOrEmpty(metaData.Description))
chkbox.Attributes.Add(“title”,metaData.Description);
返回MvcHtmlString.Create(chkbox.ToString());
}
我试过了,到目前为止似乎还有效-仍然需要尝试一些需要元素ID的示例:
public static MvcHtmlString CheckBoxForWithTitle<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string chkboxText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last();
MemberExpression memberExpression = expression.Body as MemberExpression;
string parameterName = memberExpression.Member.Name;
if (string.IsNullOrEmpty(chkboxText))
return MvcHtmlString.Empty;
var chkbox = new TagBuilder("input");
chkbox.Attributes.Add("type", "checkbox");
chkbox.MergeAttributes(new RouteValueDictionary(htmlAttributes));
if (!string.IsNullOrEmpty(metaData.Description))
chkbox.Attributes.Add("title", metaData.Description);
return MvcHtmlString.Create(chkbox.ToString());
}
public static MvcHtmlString CheckBoxForWithTitle(此HtmlHelper HtmlHelper,表达式表达式,对象htmlAttributes=null)
{
var metaData=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
字符串htmlFieldName=ExpressionHelper.GetExpressionText(表达式);
字符串chkboxText=metaData.DisplayName??metaData.PropertyName??htmlFieldName.Split('.').Last();
MemberExpression MemberExpression=expression.Body作为MemberExpression;
字符串参数Name=memberExpression.Member.Name;
if(string.IsNullOrEmpty(chkboxText))
返回MvcHtmlString.Empty;
var chkbox=新标记生成器(“输入”);
添加(“类型”,“复选框”);
合并属性(新的RouteValueDictionary(HtmlatAttributes));
如果(!string.IsNullOrEmpty(metaData.Description))
chkbox.Attributes.Add(“title”,metaData.Description);
返回MvcHtmlString.Create(chkbox.ToString());
}
您当前的实现没有考虑模型绑定和ModelState
,没有生成不引人注目的验证所需的属性,并且可能会生成不正确的id
属性
在您自己的帮助程序中使用现有的html帮助程序方法,以便生成正确的html
public static MvcHtmlString TextBoxForWithTitle<Tmodel, TProperty>(this HtmlHelper<Tmodel> htmlHelper, Expression<Func<Tmodel, TProperty>> expression, object htmlAttributes = null)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (!string.IsNullOrEmpty(metaData.Description))
{
attributes.Add("title", metaData.Description);
}
return htmlHelper.TextBoxFor(expression, attributes);
}
旁注:若要查看现有帮助程序的实际工作情况,您可以查看源代码。很遗憾,这并不完全有效。当使用常规checkboxfor并在模型中加载值时,会检查其是否为true,但新的helper不会。有人知道为什么不可以吗?谢谢Stephen!我知道我必须循环以某种方式返回到实际的帮助项!
return htmlHelper.CheckBoxFor(expression, attributes);