views:

1566

answers:

1

I have an ASPNET MVC 2 project. When I use

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

the TexBoxFor will render as

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

Field in the model is

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

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

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

Thanks to all.

+4  A: 

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