MVC 4 - Ajax - 将一个局部视图替换为另一个视图

Jam*_*s P 5 asp.net-mvc asp.net-mvc-ajax asp.net-mvc-4

我正在尝试使用部分视图替换主视图中div的内容,具体取决于单击哪个@ Ajax.ActionLink.

最初我加载_CaseLoad局部视图然后我希望能够在单击相关的ActionLink时在两个部分视图_CaseLoad和_Vacancies之间切换.我在'Main'文件夹中找到了部分视图和索引视图.

当我点击'Vacancies'链接时,它会简要显示LoadingElementID,但它不会用_Vacancies局部视图替换_CaseLoad局部视图?

控制器动作(MainController):

    public ActionResult Index(int page = 1, string searchTerm = null)
    {
        MainIndexViewModel model = new MainIndexViewModel()
        // Populate Model

        return View(model);

    }
Run Code Online (Sandbox Code Playgroud)

主要指数观点:

@model Project.WebUI.Models.MainIndexViewModel

@{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="MainIndex">
        <div id="Menu">

            @Ajax.ActionLink("CaseLoad", "_CaseLoad", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
                LoadingElementId = "busycycle" }) |

            @Ajax.ActionLink("Vacancies", "_Vacancies", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
                LoadingElementId = "busycycle" })

        </div><hr/>


        <div id="busycycle" style="display:none;"><img src="~/Content/images/preloader-w8-cycle-black.gif" /></div>
        <div id="Main">
            @Html.Partial("_CaseLoad")
        </div>

    </div>
Run Code Online (Sandbox Code Playgroud)

ara*_*rog 5

你对@ Ajax.ActionLink的调用是错误的,你这样做:

@Ajax.ActionLink("CaseLoad", "_CaseLoad", "Main", null, new AjaxOptions() { UpdateTargetId = "Main", InsertionMode = InsertionMode.Replace, HttpMethod = "GET",
            LoadingElementId = "busycycle" })
Run Code Online (Sandbox Code Playgroud)

这意味着你的链接文本是"CaseLoad",而在"MainController"中调用的动作是"_CaseLoad"?是不是"_CaseLoad"是你的局部视图的名称?

要解决这个问题,你必须在MainController中创建CaseLoad和Vacancies动作,使两个方法都返回PartialView("_ CaseLoad.cshtml",model); 或PartialView("_ Vacancies.cshtml,model")如下所示:

public ActionResult CaseLoad(int page = 1, string searchTerm = null)
{
    MainIndexViewModel model = new MainIndexViewModel()
    // Poopulate Model

    return PartialView("_CaseLoad.cshtml", model);
}
Run Code Online (Sandbox Code Playgroud)

我只是切断了所有实现细节,只是为了让您了解如何解决问题.