尝试创建一个 AJAX 操作链接,单击该链接时,应对操作方法执行 HttpGet
,该操作方法返回 PartialViewResult
并将 HTML 推送到 div 中。
这是我的观点:
<div id="admin-options" class="admin"></div>
@Ajax.ActionLink("Show Admin Options", "ShowOptions", "Post", new { area = "Admin" }, new AjaxOptions { UpdateTargetId = "admin-options", HttpMethod = "GET" })
操作方法如下:
public class PostController : Controller
{
[HttpGet]
[Authorize(Roles="Admin")]
public PartialViewResult ShowOptions()
{
return PartialView();
}
}
这是它生成的 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>
我觉得不错。
但它不是执行 AJAX 调用,而是通过浏览器 URL 执行常规 HTTP GET,并重定向到 /Admin/Post/ShowOptions。
显然我错过了一些东西 - 但什么?
最佳答案
确保您的页面中包含不显眼的 AJAX javascript 库。
<script src="<%=Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")%>" type="text/javascript"></script>
关于asp.net-mvc - ASP.NET MVC 3 (Razor) Ajax.ActionLink - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5725525/