Search code examples
validationasp.net-mvc-4http-postunobtrusive-validation

setting a globalization culture in unobtrusive true mode


In my Asp.net MVC application, i need to set the DATE TIME model field culture as "it-IT". Normally this will take system culture but to to change this defatult one i have sets UI culture to "it-IT"

i have tried below code in my application to changes the Culture

<system.web>
 <globalization uiCulture="it-IT" culture="it-IT" />

but its works if UnobtrusiveJavaScriptEnabled sets as false only. But in my application i have to maintain this value as true itself. refer below:

 <add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

Can you please anyone suggest me to set the globalization culture in unobtrusive true mode

My view section:

@Html.EJ().DatePickerFor(x => x.Date).DateFormat("dd/MM/yyyy").Locale("it-IT")

Controller:

        public ActionResult Index()
    {

        var model = new maskEdit();
        model.Date = DateTime.Now;
        return View(model);

    }
    [HttpPost]

    public ActionResult Index(maskEdit model)
    {           

        return View(model);
    }

Model:

public class maskEdit
{

    [Display(Name ="DATE CULTURE")]
    [DataType(DataType.Date)]
    public DateTime Date{ get; set; }
}

Solution

  • I found the solution on this. In my application i have added jquery unobtrusive validation. so in validation, date with format "dd/mm/yyyy" is considered invalid date. To submit the date value with format "dd/mm/yyyy", please refer the below code:

     $(document).ready(function () {
        $.culture = Globalize.culture("it-IT");
        $.validator.methods.date = function (value, element) {
                     return this.optional(element)
                || Globalize.parseDate(value, "dd/MM/yyyy", "it-IT")
                || Globalize.parseDate(value, "dd/MM/yyyy");
        }
    });