TextBoxFor渲染HTML与preFIX上的ID属性 [英] TextBoxFor rendering to HTML with prefix on the ID attribute

查看:113
本文介绍了TextBoxFor渲染HTML与preFIX上的ID属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASPNET MVC 2项目。当我使用

I have an ASPNET MVC 2 project. When I use

<%= Html.TextBoxFor(model => model.Login) %>

在TexBoxFor将呈现为

the TexBoxFor will render as

<input id="Login" name="Login" type="text" value="" />

在模型字段是

[Required(ErrorMessage = "")]
[DisplayName("Login")]
public string Login { get; set; }

我能做出的 ID 命名属性的一些preFIX?像

Can I made id and name attribute with some prefix? Like

<input id="prefixLogin" name="prefixLogin" type="text" value="" />

感谢所有。

推荐答案

这似乎MVC 2 RTM目前不提供此功能。你可以尝试这些扩展方法:

It seems MVC 2 RTM does not currently provide this feature. You can try these extension methods:

                public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression)
    {
        return ValidationMessageFor(htmlHelper, prefix, expression, null, new RouteValueDictionary());
    }

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, string validationMessage)
    {
        return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary());
    }

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, string validationMessage, object htmlAttributes)
    {
        return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, string validationMessage, IDictionary<string, object> htmlAttributes)
    {
        return htmlHelper.ValidationMessage(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)),
            validationMessage,
            htmlAttributes);
    }

    public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression)
    {
        return HiddenFor(htmlHelper, prefix, expression, (IDictionary<string, object>)null);
    }

    public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        return HiddenFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
        return htmlHelper.Hidden(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)),
            ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model,
            htmlAttributes);
        /*return HiddenHelper(htmlHelper,
                            ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model,
                            false,
                            ExpressionHelper.GetExpressionText(expression),
                            htmlAttributes);*/
    }

    public static MvcHtmlString TextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression)
    {
        return TextAreaFor(htmlHelper, prefix, expression, (IDictionary<string, object>)null);
    }

    public static MvcHtmlString TextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        return TextAreaFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString TextAreaFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }

        string value;
        var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        if (modelMetadata.Model != null)
            value = modelMetadata.Model.ToString();
        else
            value = String.Empty;

        return htmlHelper.TextArea(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)),
            value,
            htmlAttributes);
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression)
    {
        return TextBoxFor(htmlHelper, prefix, expression, (IDictionary<string, object>)null);
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        return TextBoxFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes));
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, string prefix, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
    {
        return htmlHelper.TextBox(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)),
            ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model,
            htmlAttributes);
    }

这篇关于TextBoxFor渲染HTML与preFIX上的ID属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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