重写一个asp.net MVC ActionLink的AJAX作为一个jQuery / AJAX声明 [英] Rewrite an asp.net mvc ajax actionlink as a jquery/ajax statement

查看:160
本文介绍了重写一个asp.net MVC ActionLink的AJAX作为一个jQuery / AJAX声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会如何重写只使用jQuery的阿贾克斯+这种说法?这code显示的链接。当我点击它视图返回到StuffPanel ID。

  @ {AjaxOptions选项=新AjaxOptions
           {
               InsertionMode = InsertionMode.Replace,
               UpdateTargetId =StuffPanel
               OnBegin =OnBeginWork
               的onComplete =OnCompleteWork
           };}
< P> @ Ajax.ActionLink(显示的东西,索引,东西,选项)LT; / P>


解决方案

在这里你去:

 < A HREF =/索引/东西ID =行动>显示的东西与LT; / A>$(函数(){
    $('#行动)。点击(函数(五){
        亦即preventDefault();
        VAR URL = $(本).attr('href属性);
        $阿贾克斯({
            网址:网址,
            beforeSend:OnBeginWork,
            完成:OnCompleteWork,
            成功:功能(HTML){
                $('#StuffPanel')HTML(HTML);
            }
        });
    });
});

我建议你保持ActionLink的所有它做的是让你正确的路由。一个ID添加到它,以便您可以绑定click事件更容易,然后使用click事件回调来触发Ajax调用。使用功能成功的响应,以取代 #StuffPanel 的内容。

How would you rewrite this statement using Jquery + Ajax only? This code displays a link. When I click on it a view is returned into the StuffPanel id.

@{ AjaxOptions options = new AjaxOptions
           {
               InsertionMode = InsertionMode.Replace,
               UpdateTargetId = "StuffPanel",
               OnBegin = "OnBeginWork",
               OnComplete = "OnCompleteWork",
           };}
<p>@Ajax.ActionLink("show stuff", "Index", "Stuff", options)</p>

解决方案

Here you go:

<a href="/Index/Stuff" id="action">show stuff</a>

$(function() {
    $('#action').click(function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $.ajax({
            url: url,
            beforeSend: OnBeginWork,
            complete: OnCompleteWork,
            success: function(html) {
                $('#StuffPanel').html(html);
            }
        });
    });
});

I'd recommend you keep the actionLink as all it's doing is getting the correct routing for you. Add an ID to it so that you can bind the click event easier, and then use the click event callback to fire the ajax call. Use the success function to replace the contents of #StuffPanel with the response.

这篇关于重写一个asp.net MVC ActionLink的AJAX作为一个jQuery / AJAX声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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