简体   繁体   中英

ASP.NET MVC EditorFor DateTime

How to implement EditorFor DateTime?

I already have editors for Date and Time separately. Can I use it for my DateTime editor?

Are there any suggestions, how to do it?

Date editor:

@model DateTime

@Html.TextBox(string.Empty, (Model != default(DateTime) ? Model.ToShortDateString() : string.Empty), new { @class = "date form-control" })

Time editor:

@model DateTime?

@Html.TextBox(string.Empty, (Model.HasValue ? Model.Value.ToString("HH:mm") : string.Empty), new { @class = "time form-control" })

I am using jQuery UI datepicker for date editor. Timepicker is also jQuery plugin.

Create DateTime.cshtml in Views/Shared/EditorTemplates that contains:

@inherits System.Web.Mvc.WebViewPage<System.DateTime?>
@Html.TextBox("", string.Format("{0:yyyy-MM-dd HH:mm}", 
              Model.HasValue ? Model : null), new { @class = "dateTimePicker" })

And use some jquery datepicker. think this one works with default jquery ui one.

Can I use it for my DateTime editor?

With this JQuery TimePicker plugin you can definitely have one Editor field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM