您好我有以下代码:
@Html.TextBoxFor(m => m.StartDate,"{0:MM/dd/yy hh:mm:ss}",new{@class="form-control axDateTimePicker"})
如何编写助手看起来像
@Html.DateTimePickerHelper(m=>m.StartDate)
格式字符串和类在这个新助手里面?
答案 0 :(得分:5)
为此,您需要使用custom html helpers
创建extension method
请尝试以下代码
namespace System.Web.Mvc
{
public static class CustomHtmlHelpers
{
public static MvcHtmlString DateTimePickerHelper<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
var attributes = new RouteValueDictionary(htmlAttributes);
string format = "{0:MM/dd/yy hh:mm:ss}";
return System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, format, attributes);
}
}
}