ASP.NET MVC 3 (Razor) Ajax.ActionLink - 我做错了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5725525/
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-06 10:59:51  来源:igfitidea点击:

ASP.NET MVC 3 (Razor) Ajax.ActionLink - What am i doing wrong?

asp.net-mvcajaxasp.net-mvc-3razoractionlink

提问by RPM1984

Trying to have a AJAX action link which when clicked, should do a HttpGetto an action method which returns a PartialViewResultand shoves the HTML into a div.

尝试使用 AJAX 操作链接,单击该链接时,应该对HttpGet返回 aPartialViewResult并将 HTML 推入 div的操作方法执行操作。

Here's my View:

这是我的观点:

<div id="admin-options" class="admin"></div>
@Ajax.ActionLink("Show Admin Options", "ShowOptions", "Post", new { area = "Admin" }, new AjaxOptions { UpdateTargetId = "admin-options", HttpMethod = "GET" })

Here's the action method:

这是操作方法:

public class PostController : Controller
{
   [HttpGet]
   [Authorize(Roles="Admin")]
   public PartialViewResult ShowOptions()
   {
      return PartialView();
   }
}

Here's the HTML it generates:

这是它生成的 HTML:

<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#admin-options" href="/Admin/Post/ShowOptions">Show Admin Options</a>

Looks fine to me.

对我来说看起来不错。

But instead of doing an AJAX call, it does a regular HTTP GET via the browser URL, and redirects to /Admin/Post/ShowOptions.

但它不是执行 AJAX 调用,而是通过浏览器 URL 执行常规 HTTP GET,并重定向到/Admin/Post/ShowOptions

Obviously im missing something - but what?

显然我错过了一些东西 - 但什么?

回答by Talljoe

Make sure you have the unobtrusive AJAX javascript library included in your page.

确保您的页面中包含不显眼的 AJAX javascript 库。

<script src="<%=Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")%>" type="text/javascript"></script>

回答by psy

And for those using the Razor view engine...

对于那些使用 Razor 视图引擎的人......

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

回答by Nick Albrecht

You may also want to include the InsertionMode option in the AjaxOptions. I'm sure there's a default behavior if you exclude it but it's better to explicitly define it for things like this.

您可能还希望在 AjaxOptions 中包含 InsertionMode 选项。如果您排除它,我确信有一个默认行为,但最好为这样的事情明确定义它。