I'm using client-side validation in an MVC project, but the way I'm using it is a little complicated and so I think I've missed something somewhere that is causing a break in the chain of things you need to set up in order for it to work.
As I understand it, this is the complete picture:
- You need to have the following JS libraries loaded: jQuery, jQuery Validate, and Microsoft Unobtrusive Validation
- You need to enabled client-side validation, and unobtrusive validation, in the Web.config.
- You need to apply some validation attributes to your model.
- You need to include some kind of display for the errors in your view.
I believe I've done all this, but there are some parts of my particular solution that complicate things, and maybe someone can help me sort it out.
In my case, I've included all of the JS libraries. I've checked that my Web.config looks like this:
<configuration>
[...]
<appSettings>
[...]
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
[...]
</appSettings>
[...]
</configuration>
I have applied some validation attributes to my model:
public class CreateStudentEventViewModel : IValidatableObject
{
[Required]
public DateTime StartDate { get; set; }
}
And I have included a display for the validation results in my view:
@using (Html.BeginForm([...]))
{
[...]
@Html.ValidationSummary()
[...]
}
However, when I click the submit button, a normal submit occurs. I suspect this is because of how I'm managing form submits. I'm actually loading the form via AJAX into a modal. I suspect what's happening is that because the form doesn't exist in the DOM on page load, MVC isn't attaching its submit listener to the form correctly.
So without further ado, my question is:
Is there a way to manually attach the unobtrusive validation submit listener or to manually trigger the unobtrusive validation?
OR
Am I on the wrong track, and something else is wrong?