ModelState is invalid with DateTimeOffset + MVC4

2019-08-20 18:38发布

问题:

I have an issue with model state with datetimeoffset. Saw the answers at :Modelstate always throws invalid datetime error but these doesnt solve my problem.

I have FromDate in model:

[Required, Display(Name = "From")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTimeOffset FromDate { get; set; }

I am showing it in my view:

<div class="editor-label">
@Html.LabelFor(O => O.FromDate)

-- start-- input data-val="true" id="FromDate" name="FromDate" type="datetime" class="datetime" data-datetime="" value ="@Model.FromDate.ToString("r")" ---end

@Html.ValidationMessageFor(O => O.FromDate)
</div>

Changing the datetime value in jquery to local time in jquery:

if ($(this.element).val() != "") {
            // If we wont specify time on recreate then time sliders will stay unchanged.
            //  we manipulate datepicker value and value of input to display differently.
            //  LLLL--> Thursday, April 18 2013 1:20 PM
            //  L --> 04/18/2013
            //  LT --> 8:30 PM
           this._Input.datepicker("setDate", new Date(moment($(this.element).val()).format("LLLL")));
           this._Input.val(moment($(this.element).val()).format("L LT"));
        }

However when I edit and save, I am getting "FromDate is not valid" exception.

"The value 'Fri Jun 14 05:30:00 UTC+0530 2013' is not valid for From."

can somebody pls advise how to solve?

Model Binder:

protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        //var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;

        string s = bindingContext.ValueProvider.GetValue("FromDate").AttemptedValue;
        string value = bindingContext.ValueProvider.GetValue("ThruDate").AttemptedValue;
        DateTimeOffset from;
        DateTimeOffset thru;
        if (s.IndexOf("UTC") != -1)
        {
              from = DateTimeOffset.ParseExact(s, "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy", CultureInfo.InvariantCulture);
        }
        else
        {
            from = DateTimeOffset.ParseExact(s, "ddd, dd MMM yyyy HH:mm:ss \"GMT\"", CultureInfo.InvariantCulture);

        }

        if (value.IndexOf("UTC") != -1)
            thru = DateTimeOffset.ParseExact(value, "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy", CultureInfo.InvariantCulture);
        else
            thru = DateTimeOffset.ParseExact(value, "ddd MMM dd yyyy HH:mm:ss \"GMT\"zzz \"(India Standard Time)\"", CultureInfo.InvariantCulture);



        return PrFactory.Create(Convert.ToInt32(bindingContext.ValueProvider.GetValue("PartyRoleTypeId").AttemptedValue),
            Convert.ToInt32(bindingContext.ValueProvider.GetValue("PartyId").AttemptedValue),
            from,
            Convert.ToBoolean(bindingContext.ValueProvider.GetValue("IsSoftDeleted").AttemptedValue),
            new Party(),
            new PartyRoleType(),
           thru);

        //return base.CreateModel(controllerContext, bindingContext, modelType);
    }