翻译MVC Ajax.ActionLink到.post的jQuery的方法? [英] Translate MVC Ajax.ActionLink to jQuery .post method?

查看:114
本文介绍了翻译MVC Ajax.ActionLink到.post的jQuery的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理的,使用ValidateAntiForgeryToken与AJAX这个ASP.NET MVC的问题打破了AJAX调用(<一href="http://stackoverflow.com/questions/1786500/using-validateantiforgerytoken-with-ajax-actionlink">Using ValidateAntiForgeryToken与Ajax.ActionLink )。唯一的解决方案(如该主题中所述)要求的jQuery。

我该怎么做与jQuery的。员额法下的eqvivalent?

  @ Ajax.ActionLink(执行HTTPPost,后测,新AjaxOptions {HttpMethod =POST})
 

下面是控制器:

  // [ValidateAntiForgeryToken]
公共类MoreInfoController:控制器
{
    [HttpPost]
    公众的ActionResult后测()
    {
        返回查看();
    }
}
 

下面是它的索引视图与JavaScript和HTML:

 &LT;脚本类型=文/ JavaScript的&GT;
    $(文件)。就绪(函数(){
        $('#linkToPost)点击(PostForm);
    });

    功能PostForm(){
        $。员额(/后测,$('#了testForm')。序列化(),功能(数据){

        });
        返回false;
    }

&LT; / SCRIPT&GT;
&LT;一个ID =linkToPost的href =#&GT;执行HTTP邮报&LT; / A&GT;
&LT;形式ID =了testForm方法=POST&GT;
    @ Html.AntiForgeryToken()
&LT; /形式GT;
 

这里是路线:

  routes.MapRoute(PostTest1
                            后测,
                            新{控制器=MoreInfo,行动=后测}
        );
 

解决方案

假设你的防伪造的令牌元素的形式为您想发表你可以做到以下几点:

下面是你的链接:

 &LT;一个ID =linkToPost的href =#&GT;执行HTTP邮报&LT; / A&GT;
 

下面是绑定​​该链接的点击发布形式jQuery的:

 &LT;脚本类型=文/ JavaScript的&GT;
   $(文件)。就绪(函数(){
      $('#linkToPost)点击(PostForm);
});
功能PostForm(){
    $。员额(/后测,$('#idOfyourForm')。序列化(),功能(数据){
      //做一些与该回来从后数据
    });
    返回false;
}

&LT; / SCRIPT&GT;
 

现在你需要做的是替换 #idOfYou​​rForm 与表单的实际ID:

&LT;形式ID =TESTFORM&GT; 你会让它 $('#TESTFORM')序列化()

接下来,您需要确保路由我有 /后测实际上会映射到控制器和动作。您将需要的东西是这样的:

  context.MapRoute(PostTest1
                 后测,//带参数的URL
                 新{控制器=MoreInfoController,行动=后测});
 

就是这样。

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:

<a id="linkToPost" href="#">Perform HTTP Post</a>

Here is the jquery to bind the click of this link to post the form:

<script type="text/javascript">
   $(document).ready(function() {
      $('#linkToPost').click(PostForm);   
});
function PostForm() {
    $.post("/PostTest", $('#idOfyourForm').serialize(), function(data) {
      // do something with the data that comes back from the post 
    });
    return false;
}

</script>

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:

context.MapRoute("PostTest1",
                 "PostTest",  // URL with parameters
                 new { controller = "MoreInfoController", action = "PostTest" });

That's it.

这篇关于翻译MVC Ajax.ActionLink到.post的jQuery的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆