2010-01-12 8 views
8

Ho un progetto ASPNET MVC 2. Quando usoTextBoxPer il rendering in HTML con prefisso sull'attributo ID

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

il TexBoxFor renderà come

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

campo nel modello è

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

Posso fatto id e nome attributo con un determinato prefisso? Mi piace

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

Grazie a tutti.

risposta

13

Sembra che MVC 2 RTM attualmente non fornisca questa funzionalità. È possibile provare questi metodi di estensione:

   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

voi sono una leggenda. – ajbeaven

+0

Avevo bisogno di questo perché avevo bisogno del prefisso impostato su alcuni oggetti ma non su altri. – Chris

6

È sempre possibile 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" }) %> 
+0

Qualcuno ha effettivamente verificato se funziona? Perché non penso che lo farebbe. Non penso che tu possa impostare il nome o la proprietà id usando l'oggetto anonimo usando il built-in HtmlHelper perché unisce i suoi valori sopra l'oggetto htmlAttributes. Ho creato alcuni aiutanti personalizzati per mio uso personale in cui ho riordinato l'utilizzo in modo tale che se volessi farlo potrei fare ciò che hai detto qui. –

+0

La proprietà 'id' può essere modificata ma 'nome' no. 'Nome' è sovrascritto. Il problema sarebbe che LabelFor, ValidationMessageFor e TextBoxFor (un esempio) sarebbero andati fuori sincrono se il campo id è cambiato in modo incoerente, quindi i metodi di estensione sono la strada da percorrere. –

0

ci sono diverse soluzioni differenti per lo stesso problema .. Ho creato un nuovo progetto di test MVC e copiato web.config intera di vista al vecchio progetto in cui mi è stato sempre questo errore, risolto

Problemi correlati