asp.net-mvc 如何使用 Ajax.ActionLink?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5586327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:05:45  来源:igfitidea点击:

How to use Ajax.ActionLink?

asp.net-mvcasp.net-mvc-3

提问by Shawn Mclean

First of all, where is the documentation for Ajax.*methods in asp.net mvc?

首先,Ajax.*asp.net mvc 中方法的文档在哪里?

Can Ajax.ActionLinkbe used to call an action, get a partial view, open a modal window and put the content in it?

可以Ajax.ActionLink用来调用动作、获取局部视图、打开模态窗口并将内容放入其中吗?

回答by Felix Martinez

Sure, a very similar questionwas asked before. Set the controller for ajax requests:

当然,之前有人问过一个非常相似的问题。为 ajax 请求设置控制器:

public ActionResult Show()
{
    if (Request.IsAjaxRequest()) 
    {
        return PartialView("Your_partial_view", new Model());
    }
    else 
    {
        return View();
    }
}

Set the action link as wanted:

根据需要设置操作链接:

@Ajax.ActionLink("Show", 
                 "Show", 
                 null, 
                 new AjaxOptions { HttpMethod = "GET", 
                 InsertionMode = InsertionMode.Replace, 
                 UpdateTargetId = "dialog_window_id", 
                 OnComplete = "your_js_function();" })

Note that I'm using Razor view engine, and that your AjaxOptions may vary depending on what you want. Finally display it on a modal window. The jQuery UI dialogis suggested.

请注意,我使用的是 Razor 视图引擎,并且您的 AjaxOptions 可能会根据您的需要而有所不同。最后将其显示在模态窗口上。建议使用jQuery UI 对话框

回答by komsky

@Ajax.ActionLink requires jQuery AJAX Unobtrusive library. You can download it via nuget:

@Ajax.ActionLink 需要 jQuery AJAX Unobtrusive 库。您可以通过 nuget 下载它:

Install-Package Microsoft.jQuery.Unobtrusive.Ajax

Then add this code to your View:

然后将此代码添加到您的视图中:

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

回答by Stefan Vlad

For me this worked after I downloaded AJAX Unobtrusive library via NuGet :

对我来说,这在我通过 NuGet 下载 AJAX Unobtrusive 库后起作用了:

 Search and install via NuGet Packages:   Microsoft.jQuery.Unobtrusive.Ajax

Than add in the view the references to jquery and AJAX Unobtrusive:

然后在视图中添加对 jquery 和 AJAX Unobtrusive 的引用:

@Scripts.Render("~/bundles/jquery")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"> </script>

回答by Muhammad Adeel Zahid

Ajax.ActionLink only sends an ajax request to the server. What happens ahead really depends upon type of data returned and what your client side script does with it. You may send a partial view for ajax call or json, xml etc. Ajax.ActionLink however have different callbacks and parameters that allow you to write js code on different events. You can do something before request is sent or onComplete. similarly you have an onSuccess callback. This is where you put your JS code for manipulating result returned by server. You may simply put it back in UpdateTargetID or you can do fancy stuff with this result using jQuery or some other JS library.

Ajax.ActionLink 只向服务器发送 ajax 请求。前面发生的事情实际上取决于返回的数据类型以及您的客户端脚本如何处理它。您可以发送 ajax 调用或 json、xml 等的部分视图。 然而,Ajax.ActionLink 具有不同的回调和参数,允许您针对不同的事件编写 js 代码。您可以在发送请求之前执行某些操作或onComplete. 同样,您有一个 onSuccess 回调。这是您放置用于操作服务器返回结果的 JS 代码的地方。您可以简单地将它放回 UpdateTargetID 中,或者您可以使用 jQuery 或其他一些 JS 库对这个结果做一些奇特的事情。