We have an attribute called DateReleased for which the following Data Annotation attributes are added
[Required]
[DataType(DataType.Date, ErrorMessage = "Please enter date")]
[DisplayName("Date Released")]
public object DateReleased { get; set; }
Following is the Action which is implemented to insert a new record to the database
[HttpPost]
public ActionResult Create([Bind(Exclude="Id")] Movie movie)
{
try
{
if (ModelState.IsValid)
{
_entities.AddToMovies(movie);
_entities.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
catch
{
return View();
}
}
I have enabled the client validation, by placing the following lines of code in the create view
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.validate.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMvcValidation.js"></script>
<% Html.EnableClientValidation(); %>
But I was surprised to find that only Required validation is firing on client side. Data Type validation of Date is only firing at server side. Please let me know the reason behind the failure of client side validation and what would be workarounds to fire the client side validation.
Yes. Add a custom attribute class as below
Create Client Validation Rule class
Create an adapter class which hooks in the custom attribute and client validation rule as below. Make sure of adding refernce of the above attribute class
Then modify the global.asax file
Adding the Attribute to the model class as below
Adding following client side code in the view
Hope this would help you.