自定義助手 - 日期時間選擇器
public static MvcHtmlString DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
var sb = new StringBuilder();
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var dtpId = "dtp" + metaData.PropertyName;
var dtp = htmlHelper.TextBoxFor(expression, htmlAttributes).ToHtmlString();
sb.AppendFormat("<div class='input-group date' id='{0}'> {1} <span class='input-group-addon'><span class='glyphicon glyphicon-calendar'></span></span></div>", dtpId, dtp);
return MvcHtmlString.Create(sb.ToString());
}
例:
@Html.DatePickerFor(model => model.PublishedDate, new { @class = "form-control" })
如果你使用 Bootstrap.v3.Datetimepicker 你的 JavaScript 如下所示 -
$('#dtpPublishedDate').datetimepicker({ format: 'MMM DD, YYYY' });