asp.net-mvc-3 – TextBoxFor vs EditorFor,和htmlAttributes vs additionalViewData

我创建了一个认的MVC 3项目(使用剃刀),以演示一个问题。

登录页面上,有一行:

@Html.TextBoxFor(m => m.UserName)

如果我改变为:

@Html.TextBoxFor(m => m.UserName,new { title = "ABC" })

然后它被渲染为(带有标题属性):

<input data-val="true" data-val-required="The User name field is required." id="UserName" name="UserName" title="ABC" type="text" value="" />

但是,如果我做一个EditorFor:

@Html.EditorFor(m => m.UserName,new { title = "ABC" })

然后它被渲染(没有标题属性):

<input class="text-Box single-line" data-val="true" data-val-required="The User name field is required." id="UserName" name="UserName" type="text" value="" />

所以总的来说,当我使用EditorFor时,title属性会丢失。

我知道TextBoxFor的第二个参数被称为htmlAttributes,而对于EditorFor它是additionalViewData,但我已经看到了EditorFor可以渲染这个参数提供的属性的例子。

任何人都可以解释我做错了,我怎么能有一个title属性使用EditorFor?

解决方法

我想我找到一个更好的解决方案。 EditorFor接收additionalViewData作为参数。如果你给它一个名为“htmlAttributes”的参数的属性,那么我们可以做有趣的事情:
@Html.EditorFor(model => model.EmailAddress,new { htmlAttributes = new { @class = "span4",maxlength = 128,required = true,placeholder = "Email Address",title = "A valid email address is required (i.e. user@domain.com)" } })

在模板(在这种情况下,EmailAddress.cshtml)中,您可以提供一些属性

@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue,Html.MergeHtmlAttributes(new { type = "email" }))

魔法通过这个帮助方法在一起:

public static IDictionary<string,object> MergeHtmlAttributes<TModel>(this HtmlHelper<TModel> htmlHelper,object htmlAttributes)
{
    var attributes = htmlHelper.ViewData.ContainsKey("htmlAttributes")
                            ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlHelper.ViewData["htmlAttributes"])
                            : new RouteValueDictionary();

    if (htmlAttributes != null)
    {
        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
        {
            var key = property.Name.Replace('_','-');
            if (!attributes.ContainsKey(key))
            {
                attributes.Add(key,property.GetValue(htmlAttributes));
            }
        }
    }

    return attributes;
}

当然你可以修改它来渲染属性,如果你正在做原始HTML:

public static MvcHtmlString RenderHtmlAttributes<TModel>(this HtmlHelper<TModel> htmlHelper,property.GetValue(htmlAttributes));
            }
        }
    }

    return MvcHtmlString.Create(String.Join(" ",attributes.Keys.Select(key =>
            String.Format("{0}=\"{1}\"",key,htmlHelper.Encode(attributes[key])))));
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...