Format Date On Binding (ASP.NET MVC)

后端 未结 12 724
感情败类
感情败类 2021-01-31 08:46

In my ASP.net MVC app I have a view that looks like this:

...

<%=Html.TextBox(\"due\")%>
...

I am usi

相关标签:
12条回答
  • 2021-01-31 09:14

    MVC4 EF5 View I was trying to preload a field with today's date then pass it to the view for approval.

    ViewModel.SEnd = DateTime.Now    //preload todays date  
    return View(ViewModel)           //pass to view
    

    In the view, my first code allowed an edit:

    @Html.EditedFor(item.SEnd)        //allow edit
    

    Later I changed it to just display the date, the user cannot change it but the submit triggers the controller savechanges

     <td>
     @Html.DisplyFor(item.SEnd)       //show no edit
     </td>
    

    When I changed to DisplayFor I needed to add this to ensure the preloaded value was passed back to the controller. I also need to add HiddenFor's for every field in the viewmodel.

        @Html.HiddenFor(model => model.SEnd)     //preserve value for passback.
    

    Beginners stuff but it took a while to work this out.

    0 讨论(0)
  • 2021-01-31 09:16

    I found this question while searching for the answer myself. The solutions above did not work for me because my DateTime is nullable. Here's how I solved it with support for nullable DateTime objects.

    <%= Html.TextBox(String.Format("{0:d}", Model.Property)) %>
    
    0 讨论(0)
  • 2021-01-31 09:21

    First, add this extension for getting property path:

    public static class ExpressionParseHelper
    {
        public static string GetPropertyPath<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> property)
        {                       
             Match match = Regex.Match(property.ToString(), @"^[^\.]+\.([^\(\)]+)$");
             return match.Groups[1].Value;
        }
    }
    

    Than add this extension for HtmlHelper:

     public static MvcHtmlString DateBoxFor<TEntity>(
                    this HtmlHelper helper,
                    TEntity model,
                    Expression<Func<TEntity, DateTime?>> property,
                    object htmlAttributes)
                {
                    DateTime? date = property.Compile().Invoke(model);
                    var value = date.HasValue ? date.Value.ToShortDateString() : string.Empty;
                    var name = ExpressionParseHelper.GetPropertyPath(property);
    
                    return helper.TextBox(name, value, htmlAttributes);
                }
    

    Also you should add this jQuery code:

    $(function() {
        $("input.datebox").datepicker();
    });
    

    datepicker is a jQuery plugin.

    And now you can use it:

    <%= Html.DateBoxFor(Model, (x => x.Entity.SomeDate), new { @class = "datebox" }) %>
    

    ASP.NET MVC2 and DateTime Format

    0 讨论(0)
  • 2021-01-31 09:22

    I just came across this very simple and elegant solution, available in MVC 2:

    http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx

    Basically if you are using MVC 2.0, use the following in your view.

     <%=Html.LabelFor(m => m.due) %>
     <%=Html.EditorFor(m => m.due)%>
    

    then create a partial view in /Views/Shared/EditorTemplates, called DateTime.ascx

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
    <%=Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) %>
    

    When the EditorFor<> is called it will find a matching Editor Template.

    0 讨论(0)
  • 2021-01-31 09:23

    In order to get strongly typed access to your model in the code behind of your view you can do this:

    public partial class SomethingView : ViewPage<T>
    {
    }
    

    Where T is the ViewData type that you want to pass in from your Action.

    Then in your controller you would have an action :

    public ActionResult Something(){
        T myObject = new T();
        T.Property = DateTime.Today();
    
        Return View("Something", myObject);
    }
    

    After that you have nice strongly typed model data in your view so you can do :

    <label>My Property</label>
    <%=Html.TextBox(ViewData.Model.Property.ToShortDateString())%>
    
    0 讨论(0)
  • 2021-01-31 09:25

    Why don't you use

    <% =Html.TextBox("due", Model.due.ToShortDateString()) %>
    
    0 讨论(0)
提交回复
热议问题