update field or redirect page using jquery and asp.net mvc
Im new to jquery and stuck with what i want to achieve. Heres what I want to do using jquery and asp.net mvc.
- click a submit button
- this calls an action method called LogOn in the controller Account
- if the call allows users to log in succesfully redirect to a url (sepecified by LogOn)
- if it fails replace a div(with id="error") with "sorry error occured"
so far I tried this:
$("#submit"开发者_运维百科)
.button()
.click(function () {
$.ajax({
type: "POST",
url: "Account/LogOn",
dataType: "json",
success: function (data, textStatus) {
if (data.redirect) {
// data.redirect contains the string URL to redirect to
window.location.href = data.redirect;
}
else {
// data.form contains the HTML for the replacement form
$("#error2").replaceWith(data.error);
}
}
});
});
how do I construct the relevant bits in the action method? to make this work? and is the jquery code ok? i suspect prob not.
Thanks
If you want to redirect asp.net page at same directory , you can by Jquery/Java script by this :
$("#ctl00_iframecontent_BtnCancle").click(function () {
window.location = "IncSLAList.aspx?bi=37";
});
and To redirect to Another page of project , can use :
window.location.href = "http://ImageUpload/Welcome.aspx?
Your jQuery is almost correct:
- Don't call
.button()
(unless you're using jQuery UI and want to do that) - Add
return false;
at the end of theclick
handler to prevent the browser from submitting normally.
In the action, you would either return Json(new { redirect = str })
or return Json(new { error = str })
.
精彩评论