I am using MVC4 w / JQuery and I have a form that I created using @ Ajax.BeginForm. It has required fields (defined on its view model). I am trying to validate the form before it is sent back; however, this does not seem to be the case.
I had a submit button initially, and she just submitted the form. I changed the submit button to a regular button and tried calling $ ('# formname'). Validate (), but it always returns true (even the required fields are not populated). Other views seem to work fine (which don't use Ajax.BeginForm).
I refer to jquery, jquery-ui, jquery.validate, jquery.validate.unobtrusive and jquery.unobtrusive-ajax.
Does anyone have any ideas / suggestions that I am missing?
edit ---------------------------------------------- --- --------------------------
Below is my code (this is inside the main page that links to jquery scripts):
@model AimOnlineMRA.Models.Reports.DateRangeViewModel @using (Ajax.BeginForm("GetReport", "Reports", new AjaxOptions { UpdateTargetId = "report_pane", HttpMethod = "Post"}, new {@id="parameterForm"})) { @Html.AntiForgeryToken() <script type="text/javascript"> $(document).ready(function () { ApplyTheme(); $('#btnYes').click(function() { $('#RunSpecificDateRange').val('true'); $('#yesNo').hide(); $('#dateRangeForm').show(); }); $('#btnCancel').click(function () { $('#report_pane').html('').hide(); }); $('#btnOk').click(function () { </script> <div class="margin-auto" style="width: 400px"> <div id="yesNo"> <span class="bold">Do you want to run this report for a specific date range?</span> <br /><br /> @Html.Button("Yes", "btnYes") @Html.Button("No", "btnNo") </div> <div id="dateRangeForm"> <span class="bold">Date Range</span> <br /><br /> <div class="left"> <div class="left clear-both"> @Html.LabelFor(x => x.BeginDate) <br /> @Html.TextBoxFor(x => x.BeginDate, new {@style="vertical-align:top"}) @Html.ValidationMessageFor(x => x.BeginDate) </div> <div class="left clear-both m-top-5"> @Html.LabelFor(x => x.EndDate) <br /> @Html.TextBoxFor(x => x.EndDate, new {@style="vertical-align:top"}) @Html.ValidationMessageFor(x => x.EndDate) </div> </div> <div class="left m-left-10"> </div> <div class="left clear-both m-top-10"> @Html.Button("Ok", "btnOk") @Html.Button("Cancel", "btnCancel") </div> </div> </div> }
----------------- RESOLUTION -----------------------------
call $.validator.unobtrusive.parse($('#parameterForm')); up to $('#parameterForm').valid(); fixed the problem
source share