简体   繁体   中英

DateTime Model Binder, works for IE and Chrome but not for Firefox

i have created a DefaultModelBinder to work with DateTime

the format is "dd-MM-yyyy"

this DefaultModelBinder works fine with IE and Chrome,

but it does not work in Firefox ??

any help regarding this.

Update : {As Requested}

public class DateTimeBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var vpr = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (vpr == null)
        {
            return null;

        }

        var date = vpr.AttemptedValue;

        if (String.IsNullOrEmpty(date))
        {
            return null;
        }

        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));

        try
        {
            var realDate = DateTime.Parse(date, System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB"));

            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, new ValueProviderResult(date, realDate.ToString("yyyy-MM-dd hh:mm:ss"), System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB")));

            return realDate;
        }
        catch (Exception e)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, e);
            return null;
        }

    }
}

Global.asax:

        ModelBinders.Binders.Add(typeof(DateTime), new DateTimeBinder());
        ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeBinder());

Update2: [Images Added]

Firefox : Firefox图片

This is the validation Issue in Firefox.

Internet Explorer : Internet Explorer映像

NOTE : the request is not being sent to the SERVER, this is client-side validation from the MVC , which displays the error in FIREFOX.

i got the answer to this question , umm its just BROWSER is using different culture my firefox is using culture as "English (en)" , but i am unable to sort the issue totally as i dont know how to disable this error.

i was thinking to use a hiddenfield for the Date and then a textbox so that user's may enter the date using the Jquery UI and during the submit i can process the Date and change it to yyyy-MM-dd.

Thanks for the help guys.

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