是否有可能创建一个自定义的ASP.NET MVC强类型的HTML辅助?有可能、自定义、创建一个、类型

2023-09-03 03:14:02 作者:lover煽情

我想知道是否有可能创建一个自定义的ASP.NET MVC 2强类型的HTML辅助?创建一个普通(读未强类型的)助手是简单,但我有困难创建强类型化版本。例如,我想创建一个DatePickerFor HTML辅助...

I was wondering if it is possible to create a custom strongly typed HTML Helper in ASP.NET MVC 2? Creating a regular (read not-strongly-typed) helper is straightforward but i am having difficulty creating strongly typed versions. For example, I would like to create a DatePickerFor html helper...

任何指导或片段将大大AP preciated, 先感谢您! JP

Any guidance or snippets would be greatly appreciated, Thank you in advance! JP

推荐答案

好吧,我想通了(这是pretty的简单的...)。发布我的情况下,任何人都重载持斧到这个问题。

Ok, I figured it out (and it was pretty straightforward...). Posting one of my overloads in case anyone else runs into this question.

public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
  where TModel : class
{
    var inputName = ExpressionHelper.GetExpressionText(expression);
    return htmlHelper.DatePicker(inputName);
}