【问题标题】:MVC ActionLink in the Umbraco request context?Umbraco 请求上下文中的 MVC ActionLink?
【发布时间】:2014-05-07 14:05:55
【问题描述】:

我目前正在使用 Umbraco 6.1.6 和一些 mvc 视图和控制器。 我尝试做的是:

在我的剃刀视图中,我有以下行来创建操作链接:

@Html.ActionLink("Klik to search...", "Search", "Search", new { SearchText = "searchterm" }, null)

actionlink 触发 SearchController 的 Search 方法(是一个 SurfaceController):

public ActionResult Search(string SearchText)
{
     if (String.IsNullOrEmpty(SearchText))
     {
        return RedirectToCurrentUmbracoPage();
     }

    SearchResult result = SearchManager.Instance.Search(SearchText);

    return View("SearchResults", result);
}

当我在 Html.BeginUmbracoForm 中使用文本框和提交按钮时,效果很好,但是当我使用 Html.ActionLink 时,RedirectToCurrentUmbracoPage 会引发异常(在路由值中找不到 Umbraco 路由定义,必须在Umbraco 请求的上下文)和结果页面不使用指定的样式模板。

如何在 Umbraco 请求的上下文中强制执行 ActionLink? 这是否像其他 Umbraco 页面一样带回了样式?

感谢您的宝贵时间!

【问题讨论】:

    标签: asp.net-mvc umbraco actionlink


    【解决方案1】:

    我认为你的搜索实际上是一个动作,而不是一个动作的链接。

    在你的控制器中,我们应该看到这个

    //This is to display the actual search form
    [ChildActionOnly]
    public ActionResult Search()
    {
        return PartialView("_SearchForm");
    }
    
    //This is to display the result of the search
    [HttpPost]
    [NotChildAction]
    public ActionResult Search(string SearchText)
    {
        if (String.IsNullOrEmpty(SearchText))
        {
            return CurrentUmbracoPage();
        }
        SearchResult result = SearchManager.Instance.Search(SearchText);
        return PartialView("_SearchResults", result);
    }
    

    在您的 umbraco 视图中,您应该使用 Html.Action 调用此表单

    @Html.Action("Search", "MySearchSurface")
    

    【讨论】:

      【解决方案2】:

      首先,您不应该使用表面控制器来呈现像搜索页面一样的页面。相反,它们应该用于输出页面 components,例如搜索结果。

      其次,您可以为您的搜索页面使用继承自 RenderMvcController 的控制器。

      最后,您不能真正使用Html.ActionLink 来检索由 Umbraco 发布的页面的 URL。请参阅my answer here 解释原因。相反,您需要使用 Umbraco 内容缓存,换句话说,遍历内容对象树以查找搜索页面,例如@Model.Content.Ancestors(1).Descendants("SearchPage").First().Url 或类似的东西。

      【讨论】:

      • 不太同意最后一点。我确信 Html.ActionLink 非常适合 SurfaceController。因为surfaceControllers是基于MVC的。因此它应该工作。如果不是,则 CHop 遗漏了什么,或者 Shannon 搞砸了。
      • 它们确实适用于 SurfaceControllers。我没有说他们没有。我说您无法使用Html.ActionLink 检索已发布页面的 URL。
      • 它确实有效,但页面不应用布局。我只是得到一个纯 HTML 页面。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      相关资源
      最近更新 更多