Pass a JSON format DateTime to ASP.NET MVC

后端 未结 3 1597
不知归路
不知归路 2021-02-13 12:17

We know that MVC returns DateTime for JsonResult in this format: /Date(1240718400000)/, and we know how to parse it in JS.

However, It seems that MVC doesn\

相关标签:
3条回答
  • 2021-02-13 13:02

    In your model, use this to parse the date:

    // property
    String CreateDate;
    DateTime CreateDateAsDate;
    
    // drop prefix, drop suffix, parse as long and read as ticks
    CreateDateAsDate date = new DateTime(Int64.Parse(
        CreateDate.Substring(6).Replace(")/",String.Empty)));
    
    0 讨论(0)
  • 2021-02-13 13:06

    I think using custom Model Binder will do the trick. The below model binder class will work on both cases. It will parse all dot net recognizable date string as well as JSON formatted date string. No need to change any existing code.

    public class DateTimeModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = base.BindModel(controllerContext, bindingContext);
            if (model == null && (bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(DateTime?)))
            {
                var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                if (value == null || String.IsNullOrEmpty(value.AttemptedValue))
                    model = (bindingContext.ModelType == typeof(DateTime?)) ? null : (object)DateTime.MinValue;
                else if (Regex.IsMatch(value.AttemptedValue, @"\/Date\(\d+\)\/"))
                    model = new DateTime(1970, 1, 1).AddMilliseconds(Int64.Parse(value.AttemptedValue.Substring(6).Replace(")/", String.Empty))).ToLocalTime();
                //else //Any other format
            }
            return model;
        }
    }
    

    Configure Model Binder in Application_Start of Global.asax

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //Your Existing Code....
    
            ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
            ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());
        }
    }
    

    Vote If it helps

    0 讨论(0)
  • 2021-02-13 13:11

    The problem, as you suspected, is a model binding issue.

    To work around it, create a custom type, and let's call it JsonDateTime. Because DateTime is a struct, you cannot inherit from it, so create the following class:

    public class JsonDateTime
    {
        public JsonDateTime(DateTime dateTime)
        {
            _dateTime = dateTime;
        }
    
        private DateTime _dateTime;
    
        public DateTime Value
        {
            get { return _dateTime; }
            set { _dateTime = value; }
        }
    }
    

    Change CreateDate to this type. Next, we need a custom model binder, like so:

    public class JsonDateTimeModelBinder : IModelBinder  
    { 
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
        { 
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).ToString(); 
            return new DateTime(Int64.Parse(
                value.Substring(6).Replace(")/",String.Empty))); // "borrowed" from skolima's answer
        }
    }
    

    Then, in Global.asax.cs, in Application_Start, register your custom ModelBinder:

    ModelBinders.Binders.Add(typeof(JsonDateTime), new JsonDateTimeModelBinder());
    
    0 讨论(0)
提交回复
热议问题