jquery - How to not return success when data is not valid, or exception is thrown? -
i using jquery ajax, , hit success: function (msg)...
. want able specify when hit: error: function (msg)...
the issue here seems don't have way return true
or false
, since have return view. in normal web app (unlike mvc), used return true or false, check on boolean value, , act according this.
how can specify want jump error: function (msg)
in cases? example when validation doesn't pass, don't want it's success. here jquery code:
function contactdialog(action, controller) { var url = '/' + action + '/' + controller; $("#contactdialog").dialog({ autoopen: true, hide: "fade", show: "bounce", height: $(window).height() / 2, width: $(window).width() / 2, title: "send email", buttons: [{ text: "send", click: function () { $.ajax({ url: url, data: $("form").serialize(), context: this, contenttype: 'application/html; charset=utf-8', datatype: "html", success: function (msg) { $(this).dialog("close"); sendconfirmmessage('msgsent'); }, error: function (data) { sendconfirmmessage('msgnotsent'); } }); }, }, { text: "cancel", click: function () { $(this).dialog("close"); } }] });
} , html code validation:
<div id="contactdialog" style="display: none;"> @using (ajax.beginform(null, null, new ajaxoptions { updatetargetid = "updaterdiv" }, new { id = "contactform" })) { @html.labelfor(model => model.subject) @html.textboxfor(model => model.subject, new { @class = "aboutcontrols" }) <br /> @html.validationmessagefor(model => model.subject) <br /> @html.labelfor(model => model.from) @html.textboxfor(model => model.from, new { @class = "aboutcontrols" }) <br /> @html.validationmessagefor(model => model.from) <br /> @html.labelfor(model => model.body) @html.textareafor(model => model.body, new { @class = "aboutcontrols aboutcontroltxtarea" }) <br /> @html.validationmessagefor(model => model.body) <hr /> <br /><br /> } </div>
here code forced return view:
public partialviewresult sendmail(emailmodel model) { if (modelstate.isvalid) { mailmsg = new mailmessage(model.from, receiver, model.subject, model.body); smtp.send(mailmsg); return partialview("_about"); }...
the jquery ajax code has no way of knowing when success or failure above code, since can't return true
or return false
hope making myself clear
in mvc controller action following:
var status = new httpstatuscode(); if(dataisinvalid) //whatever validation need perform { status = httpstatuscode.badrequest; } // other validation return new httpstatuscoderesult(status);
if need change doesn't trigger error
in ajax, either leave status
null or set httpstatuscode.ok
Comments
Post a Comment