asp.net
  • asp.net-mvc-3
  • 2011-05-04 21 views 0 likes 
    0

    我有下面的語句在我看來:移動從我的觀點有些服務器代碼的的HtmlHelper

    window.location = '@Html.Raw(Url.Action("SearchAffaires","Search", (SearchCriteriaAffaire)Session["SearchCriteriaAffaire"]))' 
    

    我如何通過使用幫助改變這個?喜歡的東西:

    筆者認爲:

    window.location = '@Html.NavigateSearchPage()' 
    

    在HtmlHelpers.cs:

    public static string NavigateSearchPage(this HtmlHelper helper) 
        { 
         // what do I have to code here? 
        } 
    

    或者,也許有更好的辦法嗎?

    回答

    0

    UrlHelper擴展在這種情況下比HTML助手更有意義。

    你能做到這一點,如下所示:

    public static string SearchPage(this UrlHelper helper) 
        { 
            return helper.Action("SearchAffaires", 
           "Search", 
           (SearchCriteriaAffaire)Session["SearchCriteriaAffaire"]); 
        } 
    

    查看:上面的helper.Action功能

    window.location = '@Html.Raw(Url.SearchPage())'; 
    
    +0

    缺失;從helper.Action上面。 – 2011-05-04 08:35:42

    +0

    @Chris:哎呀!謝謝。 – 2011-05-04 08:37:20

    +0

    這就是我正在尋找:) – Bronzato 2011-05-04 08:47:39

    1

    如果你想生成的鏈接,讓你的助手返回HtmlString,使用UrlHelper代替HtmlHelper俺們只需撥打您使用在視圖中相同的方法:

    public static HtmlString NavigateSearchPage(this UrlHelper helper) 
    { 
        return helper.Action("SearchAffaires","Search", 
         (SearchCriteriaAffaire)Session["SearchCriteriaAffaire"]); 
    } 
    
    +0

    太多了右括號。 – 2011-05-04 08:35:24

    +0

    @Chris - 修正了,謝謝。我剛剛從OP的問題複製/粘貼它:) – 2011-05-04 08:39:14

    +0

    沒有問題。也這麼覺得。原來有一個Url.Raw功能,它是額外的來源。 – 2011-05-04 08:50:17

    0

    那你要的方式走。

    public static string NavigateSearchPage(this HtmlHelper helper) 
    { 
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext) 
        return helper.Raw(urlHelper.Action("SearchAffaires","Search", (SearchCriteriaAffaire)helper.ViewContext.HttpContext.Session["SearchCriteriaAffaire"]))); 
    } 
    
    相關問題