无法使用 AJAX 在 MVC asp.net 呈现部分视图

本文关键字:net 现部 视图 asp MVC AJAX | 更新日期: 2023-09-27 18:36:14

>问题是:我确实有 Index.cshtml 作为

@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="@Url.Content("../../Scripts/jquery.unobtrusive-ajax.min.js") type="text/javascript"></script>
<h2>Answer's are </h2>
<div id="result">
@Ajax.ActionLink("Search Wiki",
                  "WikiAns",
                new AjaxOptions{
                    UpdateTargetId = "result",
                InsertionMode=InsertionMode.Replace,
                HttpMethod="GET",
                LoadingElementId="progress"
                })
</div>
<div id="progress" style="display: none">
<img src="@Url.Content("../../Content/Images/ajax-loader.gif")" alt="loader" />
</div>

现在我确实有部分视图模型_WikiAns

@model IEnumerable<Questions.Models.WikiAnsClass>
    <h2>Our Links</h2>
    <table>
    @foreach (var item in Model) {
        <tr>
            <td>
            <a href="@item.url" target="_blank">@item.content</a>
            </td>
        </tr>
    }
    </table>

现在使用上面我想要的是...单击搜索 Wiki 操作链接时,id="result" 获取其中呈现的所有部分视图。但它没有发生。而不是转到页面"localhost:80/Search/WikiAns"并在那里显示结果。但我希望它保持在localhost:80/Search并替换其"结果"ID。 但它不是这样工作的。

这是我的行动,叫做WikiAns

public ActionResult WikiAns()
{
 //Code to follow...
 var wikians = //code to follow
  return PartialView("_wikiAns", wikians); 
 }

怎么了?

另外我如何实现这个 Ajax GIF 加载器...我的意思是在执行操作时显示它。?

请建议

谢谢

无法使用 AJAX 在 MVC asp.net 呈现部分视图

@Pbirkoff可能是对的,

但你可以通过我的帖子,非常容易实现你的场景演示并从那里构建它。

http://mazharkaunain.blogspot.com/2011/05/aspnet-mvc-razor-render-partial-view.html

http://mazharkaunain.blogspot.com/2011/04/aspnet-mvc-render-partial-view-using.html

您需要

在页面或布局中包含Microsoft Ajax 脚本。