I am new to ASP.Net MVC 3, facing some issues while trying to implementing client side unobtrusive validation for a editor template I have created for showing date in a cust
After some effort, I made the control to work, putting in if it is helpful to anybody out there.
The first point is to have the control defined as
@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd") : string.Empty))
/
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM") : string.Empty))
/
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("yyyy") : string.Empty))
dd
mm
yyyy
Make sure, you give empty strings as name to all the three textboxes, this forces the MVC framework to generate same name corresponding to Model DateofBirth for all the three textboxes in the client side. Also, unobtrusive javascript validation parameters would be generated for the first textbox in the list, since it is the first occurence of a edit control with the name of the model.
On the client side, on any event on any of these textboxes fires all the relevant validation events, since all these 3 textboxes have the same name.
On top of the regular validations like required,.... we have to write a custom client validation routine which would combine all these three values and checks whether the combined value forms a valid date. This can be achieved as in the answer given by Jeff.