I am dealing with this ASP.NET MVC problem of that using ValidateAntiForgeryToken with AJAX breaks the AJAX call (Using ValidateAntiForgeryToken with Ajax.ActionLink). The only solution (as stated in that topic) requires jQuery.
How do i do the eqvivalent of the following with jQuery's .post method?
@Ajax.ActionLink("Perform HTTPPost", "PostTest", new AjaxOptions{ HttpMethod="POST"})
Here is the Controller:
// [ValidateAntiForgeryToken]
public class MoreInfoController : Controller
{
[HttpPost]
public ActionResult PostTest()
{
return View();
}
}
Here is it's Index VIEW with both Javascript and HTML:
<script type="text/javascript">
$(document).ready(function () {
$('#linkToPost').click(PostForm);
});
function PostForm() {
$.post("/PostTest", $('#testForm').serialize(), function(data) {
});
return false;
}
</script>
<a id="linkToPost" href="#">Perform HTTP Post</a>
<form id="testForm" method="POST" >
@Html.AntiForgeryToken()
</form>
And here is the Route:
routes.MapRoute("PostTest1",
"PostTest",
new {controller = "MoreInfo", action = "PostTest"}
);
Assuming your anti forgery token element is in the form you want to post you can do the following:
Here is your link:
Here is the jquery to bind the click of this link to post the form:
Now what you need to do is replace
#idOfYourForm
with the actual id of your form:<form id="TestForm">
you would make it$('#TestForm').serialize()
Next you need to make sure the route I have of
/PostTest
will actually map to your controller and action. You will need something like this:That's it.
This works:
And the View:
where [UseAntiForgeryTokenOnPostByDefault] is http://weblogs.asp.net/srkirkland/archive/2010/04.aspx
My example is using $.ajax but you can easily change it to $.post
If you don't want to give the anchor an id, you could use a class selector:
Instead of doing
new { id = "someId" }
you would need to donew { @class = "ajax-link" }
or whatever you wanted your class to be named.edit
I was using the wrong overload. I was trying to set the id for the html attribute, not the route data.