asp.net mvc - Ajax.BeginForm and validation -
client side validation not working me in ajax.beginform
this code:
<div id="report"> <div id="projectreport"> <div > @{ html.enableclientvalidation(); } @using (ajax.beginform("analyticsdates", new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "reportcontent" })) { @html.labelfor(m => m.startdate) @html.textboxfor(m => m.startdate, new { id = "start" }) @html.validationmessagefor(model => model.startdate) @html.labelfor(m => m.enddate) @html.textboxfor(m => m.enddate, new { id = "end" }) @html.validationmessagefor(model => model.enddate) <input id="btnsearch" type="submit" value=@titles.search class="iconheader"/> } </div> </div> <div id="reportcontent"> </div> </div>
and enabled validation in web.config page:
<add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" />
and added js files well
<script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
second issue related first one, action
[httppost] [authorize(roles = "xxxreport")] public async task<actionresult> analyticsdates(reportrequestvm reportrequestvm) { if (!modelstate.isvalid) { return view("**mainreports**", reportrequestvm); } // fill reportrequestvm data return view("**partialreport**", reportrequestvm); }
if model valid return partial view , page looks fine , otherwise return main view , form , in page renders self twice. question is, in case client validation fails,how return main form validation errors ?
any appreciated, 10x rony
i figured out... should have partial view on result , on query.
and on failure should return "http bad request" , use following set validation on search partial view.
this how should look:
@using (ajax.beginform("cloudanalyticsdates", new ajaxoptions { insertionmode = insertionmode.replace, updatetargetid = "reportcontent", onfailure = "oncloudanalyticsfailure", onbegin = "validateform", })) { @html.labelfor(m => m.startdate) @html.textboxfor(m => m.startdate, new { id = "start" }) @html.validationmessagefor(model => model.startdate) @html.labelfor(m => m.enddate) @html.textboxfor(m => m.enddate, new { id = "end" }) @html.validationmessagefor(model => model.enddate) <input id="btnsearch" type="submit" value=@titles.search class="iconheader"/> } </div> </div> <script type="text/javascript"> $(document).ready(function () { $("#datepicker").kendodatepicker(); $("#start").kendodatepicker().data("kendodatepicker"); $("#end").kendodatepicker().data("kendodatepicker"); }); function oncloudanalyticsfailure(parameters) { $('#projectreport').html(parameters.responsetext); $('#reportcontent').empty(); cleanvalidationerror('form'); } </script>
and on server should like:
[httppost] public async task<actionresult> cloudanalyticsdates(reportrequestvm reportrequestvm) { if (!modelstate.isvalid) { response.statuscode = (int)httpstatuscode.badrequest; return partialview("_reportquery", reportrequestvm); } reportrequestvm.bomtotals = await commonrequestshelper.getbomtotalanalytics(bomtotalrequest); return partialview("_projectreport", reportrequestvm); }
Comments
Post a Comment