9

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.

msi
  • 3,202
  • 4
  • 27
  • 37

3 Answers3

13

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
  • 8,968
  • 43
  • 56
7

You can always set htmlAttributes, although it's not the cleanest way to do it.
And, you would have to do it in all of your helpers.

 <%: Html.TextBoxFor(model => model.Login, new { @id = "prefixLogin" }) %>
timss
  • 9,982
  • 4
  • 34
  • 56
Anonymous
  • 71
  • 1
  • 1
  • Has anyone actually verified if this works? Because I don't think it would. I don't think you can set the name or id property using the anonymous object using the built in HtmlHelper because it merges its values over top your object htmlAttributes. I created some custom helpers for my own use where I reordered the usage so that if I want to that I could do what you said here. – Chris Marisic May 19 '11 at 20:01
  • The 'id' property can be changed but 'name' can't. 'Name' is overwritten. The issue would be that LabelFor, ValidationMessageFor and TextBoxFor (an examples) would get out of sync if the id field is changes inconsistently, so extension methods are the way to go. – Scott Forsyth Jul 13 '11 at 21:07
0

There are different different solution for same issue.. I've created a new mvc test project and copied entire view's web.config to the old project where I was getting this error, solved

vinayak hegde
  • 2,117
  • 26
  • 26