【问题标题】:Using ajax ActionLink in place of Html.ActionLink MVC使用 ajax ActionLink 代替 Html.ActionLink MVC
【发布时间】:2014-08-16 08:11:51
【问题描述】:

全部,

我有一个 Html.Actionlink,它在我的控制器中调用一个方法并且应该返回一个局部视图。我的局部视图显示在Index.cshtml中,局部视图是_ServerStatusList.cshmtl。

查看

@using (Html.BeginForm())
{
    @Html.ActionLink("Start", 
                     "StartServer", 
                      null,
                      new { @class = "startButton" })
}

控制器

[HttpGet]
        public PartialViewResult StartServer(Model model)
        {
            model.StartServer("server01");
            return PartialView("~/Views/Home/_ServerStatusList.cshtml", model.Servers);
        }

现在单击启动按钮后,所有功能在启动服务器时都可以正常工作,但返回的视图不正确。单击后,我被重定向到 localHost/home/StartServer 并在空白页面上显示“正在运行”(作为启动的服务器应该在我的服务器列表中显示)。然后,如果我通过地址栏手动导航到我的服务器状态页面,它会在我的 _ServerStatusList.cshtml 中显示服务器正在运行(正如它应该说的那样)。

我在项目的另一部分中使用了 Ajax ActionLink 来单击按钮并返回局部视图。我也使用此代码尝试了此按钮。

 <input id="StartButton" type="image" value="submit" src="~/Images/start.png" alt="Start" height="25" />
@Ajax.ActionLink("Start", "StartServer",
                new AjaxOptions
                {
                    HttpMethod = "GET",
                    InsertionMode = InsertionMode.Replace,
                    UpdateTargetId = "CurrentView"
                }
            )

单击按钮不起作用,但单击“开始”一词会执行类似于 Html.ActionLink 的操作。如果我单击“开始”链接,它会将我带到 Index.cshtml,显示在部分视图应位于的区域中运行,但不加载 _ServersStatusList.cshtml。再次手动重定向时,一切正常,运行并正确格式化。

如何让 Ajax ActionLink 使用图像作为按钮,并在单击图像时返回正确的局部视图?

谢谢

【问题讨论】:

    标签: ajax asp.net-mvc razor


    【解决方案1】:

    MVC 足够聪明,可以找到您需要的视图,因此将返回的 PartialView 更改为:

    PartialView("~/Views/Home/_ServerStatusList.cshtml", model.Servers);
    //To
    PartialView("_ServerStatusList", model.Servers);
    

    这应该返回正确的视图:)。

    【讨论】:

    • 谢谢。原来我看得太深了-__-
    • :) 很高兴这能解决您的问题,有时我们只需要另一双眼睛,而堆栈溢出有很多。
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    相关资源
    最近更新 更多