Reputation: 593
im using MVC3. im trying to redirectToAction after doing a jquery post.
edit: sorry for not being specific. Whenever it reaches the return RedirectToAction, it just refreshes the same page and returns to the same current view.
my View code is as follows:
$("button[name='command']").click(function () {
var checked_status = null;
if ($("#select_all").is(':checked')) {
checked_status = true;
} else {
checked_status = false;
}
$.post('@Url.Action("SaveInvitations", "Contractors")',
{
command: $(this).val(),
selectedContractors: $("input[name='selectedContractors']:checked").map(function () { return $(this).val(); }).get().join(","),
page: $(".selected").text(),
CheckedAll: checked_status
});
});
and my controller has the following:
if (command == "skip")
{
TempData["message"] = string.Format("No contractors where selected");
RedirectToAction("Index", "Home");
}
else
{
...
TempData["message"] = string.Format("The invitations have been send successfully.");
Session["subModel"] = null;
return RedirectToAction("Index", "Home");
}
Then again, im just trying to do a redirectToAction after a jquery $.post. What do I have to do to make it work?
Upvotes: 0
Views: 20223
Reputation: 218722
RedirectToAction is a server side method. Since you are doing a jQuery post, you probably need to do that once your post is completed.
$.post('@Url.Action("SaveInvitations", "Contractors")',
{
command: $(this).val(),
selectedContractors: $("input[name='selectedContractors']:checked").map(function () { return $(this).val(); }).get().join(","),
page: $(".selected").text(),
CheckedAll: checked_status
},function(){
window.location.href='@Url.Action("Index", "Home")';
});
Here is another way to do write the above method, using the explicit call to done
to make it more readable.
var url='@Url.Action("SaveInvitations", "Contractors")';
var data = {
command: $(this).val(),
selectedContractors: $("input[name='selectedContractors']:checked")
.map(function () { return $(this).val(); }).get().join(","),
page: $(".selected").text(),
CheckedAll: checked_status
};
$.post(url,data)
.done(function(){
window.location.href='@Url.Action("Index", "Home")';
});
If you are doing a redirect after the jQuery ajax operation, Why not directly call the Action method (normal form posting) and then do redirect from there ? People usually use jQuery ajax operations to have partial page updates. are you sure you want to do like this ?
Upvotes: 9