29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

public JsonResult ValidateDate(string Date) {<br />

DateTime parsedDate;<br />

}<br />

}<br />

}<br />

if (!DateTime.TryParse(Date, out parsedDate)) {<br />

return Json("Please enter a valid date (mm/dd/yyyy)",<br />

JsonRequestBehavior.AllowGet);<br />

} else if (DateTime.Now > parsedDate) {<br />

return Json("Please enter a date in the future",<br />

JsonRequestBehavior.AllowGet);<br />

} else {<br />

return Json(true, JsonRequestBehavior.AllowGet);<br />

}<br />

Actions methods that support remote validation must return the JsonResult type, which tells the MVC Framework that I<br />

am working with JSON data, as explained in Chapter 23. In addition to the result, validation action methods must define a<br />

parameter that has the same name as the data field being validated: this is Date for the example. I make sure that I can parse a<br />

DateTime object from the value that the user has submitted and, if I can, check to see that the date is in the future.<br />

Tip I could have taken advantage of model binding so that the parameter to my action method would be a DateTime<br />

object, but doing so would mean that the validation method wouldn’t be called if the user entered a nonsense value like apple,<br />

for example. This is because the model binder wouldn’t have been able to create a DateTime object from apple and throws<br />

an exception when it tries. The remote validation feature doesn’t have a way to express that exception and so it is quietly<br />

discarded. This has the unfortunate effect of not highlighting the data field and so creating the impression that the value that the<br />

user has entered is valid. As a general rule, the best approach to remote validation is to accept a string parameter in the action<br />

method and perform any type conversion, parsing, or model binding explicitly.<br />

I express validation results using the Json method, which creates a JSON-formatted result that the client-side remote<br />

validation script can parse and process. If the value that I am processing meets my requirements, then I pass true as the<br />

parameter to the Json method, like this:<br />

...<br />

return Json(true , JsonRequestBehavior.AllowGet);<br />

...<br />

If I am unhappy with the value, I pass the validation error message that the user should see as the parameter, like this:<br />

...<br />

return Json("Please enter a date in the future" ,<br />

JsonRequestBehavior.AllowGet);<br />

...<br />

In both cases, I must also pass the JsonRequestBehavior.AllowGet value as a parameter. This is because the<br />

MVC Framework disallows GET requests that produce JSON by default, and I have to override this behavior to handle the<br />

validation request. Without this additional parameter, the validation request will quietly fail, and no validation errors will be<br />

displayed to the client.<br />

To use the remote validation method, I apply the Remote attribute to the property I want to validate in the model class. In<br />

Listing 25-25, you can see how I have applied the attribute to the Date property.<br />

Listing 25-25. Using the Remote Attribute in the Appointment.cs File<br />

using System;<br />

688

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!