TextBoxPer il rendering in HTML con prefisso sull’attributo ID

Ho un progetto ASPNET MVC 2. Quando lo uso

 model.Login) %> 

il TexBoxFor renderà come

  

Il campo nel modello è

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

Posso fare id e attribuire un nome con un prefisso? Piace

  

Grazie a tutti.

Sembra che MVC 2 RTM al momento non fornisca questa funzionalità. Puoi provare questi metodi di estensione:

  public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return ValidationMessageFor(htmlHelper, prefix, expression, null, new RouteValueDictionary()); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage) { return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary()); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage, object htmlAttributes) { return ValidationMessageFor(htmlHelper, prefix, expression, validationMessage, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString ValidationMessageFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, string validationMessage, IDictionary htmlAttributes) { return htmlHelper.ValidationMessage(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), validationMessage, htmlAttributes); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return HiddenFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return HiddenFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString HiddenFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary 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(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return TextAreaFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return TextAreaFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary 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(this HtmlHelper htmlHelper, string prefix, Expression> expression) { return TextBoxFor(htmlHelper, prefix, expression, (IDictionary)null); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, object htmlAttributes) { return TextBoxFor(htmlHelper, prefix, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, string prefix, Expression> expression, IDictionary htmlAttributes) { return htmlHelper.TextBox(String.Format("{0}.{1}", prefix, ExpressionHelper.GetExpressionText(expression)), ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, htmlAttributes); } 

Puoi sempre impostare htmlAttributes , anche se non è il modo più pulito per farlo.
E dovresti farlo in tutti i tuoi aiutanti.

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

Ci sono diverse soluzioni diverse per lo stesso problema. Ho creato un nuovo progetto di test di mvc e copiato l’intero web.config della vista sul vecchio progetto dove stavo ottenendo questo errore, risolto