Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

c# - Is it possible to create a custom ASP.NET MVC strongly typed HTML Helper?

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...

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...