TextBoxFor VS EditorFor和htmlAttributes VS additionalViewData [英] TextBoxFor vs EditorFor, and htmlAttributes vs additionalViewData

查看:143
本文介绍了TextBoxFor VS EditorFor和htmlAttributes VS additionalViewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立(使用剃刀),默认的MVC 3项目,以证明一个问题。

I have created a default MVC 3 project (using razor), in order to demonstrate an issue.

在登录页,有一行:

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

如果我更改为:

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

然后,它呈现为(带有标题属性):

Then the it is rendered as (with a title attribute):

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

不过,如果我让一个EditorFor:

However, if I make it an EditorFor:

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

然后,它被渲染(没有title属性)为:

Then it gets rendered (without a title attribute) as:

<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属性都将丢失。

So in summary, the title attribute is lost when I use EditorFor.

我知道,TextBoxFor第二个参数被称为htmlAttributes,并EditorFor是additionalViewData,但是我已经看到了例子,EditorFor可以使用这个参数提供的属性。

I know that the second parameter for TextBoxFor is called htmlAttributes, and for EditorFor it is additionalViewData, however I've seen examples where EditorFor can render attributes supplied with this parameter.

任何人都可以请解释什么,我做错了,我怎么能有一个标题属性使用EditorFor什么时候?

Can anyone please explain what I am doing wrong, and how I can have a title attribute when using EditorFor?

推荐答案

我想我找到了一个更好一点解决的办法。 EditorFor取入additionalViewData作为参数。如果你给它一个名为htmlAttributes同属性的参数,那么我们可以做有趣的事情吧:

I think I found a little nicer solution to it. EditorFor takes in additionalViewData as a parameter. If you give it a parameter named "htmlAttributes" with the attributes, then we can do interesting things with it:

@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)然后可以提供几个默认属性:

In the template (in this case, EmailAddress.cshtml) you can then provide a few default attributes:

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

魔术通过这个helper方法走到一起:

The magic comes together through this helper method:

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:

Of course you could modify it to render the attributes as well if you are doing raw HTML:

public static MvcHtmlString RenderHtmlAttributes<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 MvcHtmlString.Create(String.Join(" ",
        attributes.Keys.Select(key =>
            String.Format("{0}=\"{1}\"", key, htmlHelper.Encode(attributes[key])))));
}

这篇关于TextBoxFor VS EditorFor和htmlAttributes VS additionalViewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆