2010-12-02 2 views
3

Теперь я узнал, как создавать собственные HTML хелперыКак создать собственный строго типизированный html-помощник?

using System; 
namespace MvcApplication.Helpers { 
    public class InputlHelper { 
    public static string Input(this HtmlHelper helper, string name, string text) { 
     return String.Format("<input name='{0}'>{1}</input>", name, text); 
    } 
    } 
} 

Теперь, как превратить его в строго типизированных вспомогательный метод InputFor Как это в рамках?

Мне не нужен метод Html.TextBoxFor, я знаю, что он существует. Мне просто интересно, как реализовать это поведение самостоятельно и использовать это как простой пример.

PS. Я искал исходный код mvc, но не смог найти след этого таинственного TextBoxFor. Я нашел только TextBox. Я смотрю неправильно code?

ответ

4

Здесь вы найдете ASP.NET MVC 2 RTM Source code.

Если вы посмотрите на InputExtensions класса внутри System.Web.Mvc.Html имен вы найдете следующий код

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) { 
    return htmlHelper.TextBoxFor(expression, (IDictionary<string, object>)null); 
} 

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) { 
    return htmlHelper.TextBoxFor(expression, new RouteValueDictionary(htmlAttributes)); 
} 

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] 
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) { 
    return TextBoxHelper(htmlHelper, 
          ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model, 
          ExpressionHelper.GetExpressionText(expression), 
          htmlAttributes); 
} 
Смежные вопросы