I'm having problems with the date fields of my application.
Followingthisresponse,whichIfoundtobethemostappropriate, Error message in datetime field , I set up my ASP.NET MVC application as follows:
Install-Package jquery-globalize
; I installed the package jquery.validation.globalize via Install-Package jquery.validation.globalize
;
IaddedtheWeb.configtabtothelanguage..
<system.web>...<globalizationuiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true"
requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />
...
</system.web>
Although the answer indicates to add in
<configuration>
, the application reported error in the Web.config file and then I got in the<system.web>
tag.
I set up my BundleConfig.cs :
Iorganizedmyscriptsaccordingtotheresponseexample,thusleaving:
The script with Globalize.culture("pt-BR");
at the end of the image was one last attempt I made.
Jquery.validation.js I load the other views, when I need them, via @section footerSection{ Scripts.Render("~/js/jqueryval"); }
.
So my script output looks like this, in debug mode:
SoherearethepropertiesofmyViewModelusedtorenderthefields:
[DataType(DataType.Date)][DisplayName("Data Vencto.")]
[Required(ErrorMessage = "Informe o campo {0}")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? txtDataVencimento { get; set; }
[DataType(DataType.Date)]
[DisplayName("Data Pagto.")]
[Required(ErrorMessage = "Informe o campo {0}")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? txtDataPagamento { get; set; }
I already tested with DataFormatString = "{0:yyyy-MM-dd}"
, but it also did not work.
Razor:
<div class="form-group">
@Html.LabelFor(model => model.txtDataVencimento, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.txtDataVencimento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.txtDataVencimento, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.txtDataPagamento, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.txtDataPagamento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.txtDataPagamento, "", new { @class = "text-danger" })
</div>
</div>
Then I ask you for help: What may still be missing or wrong?
EDITION
According to the response from Cigano Morrison , the order of my scripts should be different and I applied the Bundle accordingly.
var bundle = new ScriptBundle("~/js/jqueryval") { Orderer = new AsIsBundleOrderer() };
bundle
.Include("~/Static/js/jquery.validate.js")
.Include("~/Static/js/jquery.validate.unobstrusive.js")
.Include("~/Static/js/globalize/globalize.js")
.Include("~/Static/js/jquery.validate.globalize.js");
bundles.Add(bundle);
With this my scripts were in the order indicated:
However,theerrorstillremains,butdisplayingamessageinEnglish:" Please enter a valid date. "
Thatwasinbold,black.
EDITION
Wheneditingmybundlestomakethemlooklikethis:
publicstaticvoidRegisterBundles(BundleCollectionbundles){bundles.Add(newScriptBundle("~/js/jquery").Include(
"~/Static/js/jquery-{version}.js",
"~/Static/js/jquery.plugin.js"));
bundles.Add(new ScriptBundle("~/js/jqueryval").Include(
"~/Static/js/jquery.validate.js",
"~/Static/js/jquery.validate.unobtrusive.js",
"~/Static/js/globalize/globalize.js",
"~/Static/js/jquery.validate.globalize.js"));
bundles.Add(new ScriptBundle("~/js/bootstrap").Include(
"~/Static/js/bootstrap.js"));
bundles.Add(new StyleBundle("~/Static/css/styles").Include(
"~/Static/css/bootstrap.css",
"~/Static/css/site.css"));
}
And with the default sort order made by BundleConfig, my scripts were in that order:
Datevalidationhaspassed,butvaluehasgoneawry: