I have a small MVC2 app that is displayed in two cultures: N-US and SX-MX. One part contains a user input for the date in which the model is pre-populated with the current date.
When using N-US, the date field is displayed as MM / DD / YAY and can be changed using the same format without any validation errors.
S-MX, Date field is displayed as DD / MM / YY, but when the date is edited in this format, the server fails with the side verification message:
Value is not valid for the date '17 / 05/1 991 '.
One of the first things that jumps out of me about that message is not localized. Both messages (which I do not think I can control) and the field's display name (which I can control and local in my code) Must appear in the local format.
I have tried to take action through code to see if verification is failing, but it seems that what is happening inside some compiled MVC or DataAnnotations code which I am not IIS6, ASP.NET 3.5 (C #), MVC 2 RM
Sample Model Code:
Public class TestVieModel {LocalizedDisplayNameDisplayName ("TheDateDisplayName", NameResourceType = typeof (Resources.Model.TestViewModel)) [Required (Error MessageResourcename = "That Validation Message Warrant") Ermmez Resource type = TypeViewModel) [datatype (datatype.dat)] Public Datetime Timecode {Received; Set; Sample Controller Action Code: [HTTP Post] [Legitimate Antifreeze Token] Public ActioncellSlave Save (Test Visual Model) {if (ModelState.IsValid) {/ / & lt; --- Always lying when using the date of installation as SX-MX and DD / MM / YAY. // return other goods. See ("full", model); } // Validation failed, show the form again. Back it See ("Enter", model); }
View sample code:
& lt;% @ page language = "c #" inheritance = "system.web.mvc viewpage "& Gt%; & Lt ;; DOCTYPE HTML PUBLIC "- // W3C // DTT XHTML 1.0 Transcription // N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Head runat = "server" & gt; & Lt; Title & gt; Test & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; Use of & lt;% (Html.BeginForm ()) {%> & Lt;% = Html Valid (% true)% & gt; & Lt; Fieldset & gt; & Lt; Story & gt; Fields & lt; / Narrative & gt; & Lt; Div class = "editor-label" & gt; & Lt;% = Html.LabelFor (model = & gt; model.The date)%> & Lt; / Div & gt; & Lt; Div class = "editor-field" & gt; & Lt;% = Html.EditorFor (model = & gt; model.The date)% & gt; & Lt;% = HTML.ValidationMessageFor (model = & gt; model.The date)%> & Lt; / Div & gt; & Lt; P & gt; & Lt; Input type = "submit" value = "save" /> & Lt; / P & gt; & Lt; / Fieldset & gt; & Lt;%}% & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Here's how I solved this matter in my case. I manually agree to the date of the controller and reset the modelstate for that property:
[HTTP post] [legitimate antigenic token] Public Action Result Service (Test Visual Model) {Var tempDate = new DateTime (); Var Culture = CultureInfo Current uk agriculture; If (Datetime Tripers (Request.form ["Thdate"], culture, date time style. One, outside tempDate) {model.DateOfBirth = tempDate; ModelState.Remove ("TheDate"); } If (ModelState.IsValid) {// & lt; --- Now return the legitimate // other goods. See ("full", model); } // Validation failed, show the form again. Back it See ("Enter", model); }
Comments
Post a Comment