TextBoxFor呈现为HTML,ID属性带有前缀

时间:2010-01-12 20:08:49

标签: c# asp.net-mvc-2

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

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

TexBoxFor将呈现为

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

模型中的字段是

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

我可以使用一些前缀创建 id 名称属性吗?像

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

感谢所有人。

3 个答案:

答案 0 :(得分:13)

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

                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);
    }

答案 1 :(得分:6)

你可以随时设置htmlAttributes,尽管这不是最干净的方法 并且,您必须在所有助手中完成。

 <%: Html.TextBoxFor(model => model.Login, new { @id = "prefixLogin" }) %>

答案 2 :(得分:0)

同一问题有不同的解决方案.. 我已经创建了一个新的mvc测试项目并将整个视图的web.config复制到我收到此错误的旧项目中,已解决