我有幾個頁面列出了搜索結果,對於我想要顯示的每個結果我想創建一個自定義的View Helper以避免重複顯示代碼。如何從自定義助手使用ASP.NET MVC Html Helpers?
如何從我的自定義視圖助手訪問方便的現有視圖助手?即在我的自定義視圖幫助器中,我想使用Url.Action(),Html.ActionLink等。我如何從我的自定義視圖幫助器中訪問它們?
using System;
namespace MvcApp.Helpers
{
public class SearchResultHelper
{
public static string Show(Result result)
{
string str = "";
// producing HTML for search result here
// instead of writing
str += String.Format("<a href=\"/showresult/{0}\">{1}</a>", result.id, result.title);
// I would like to use Url.Action, Html.ActionLink, etc. How?
return str;
}
}
}
using System.Web.Mvc
可以訪問HtmlHelpers
,但像ActionLink的便捷方法不似乎存在。
我明白你的觀點並表示同意。但是,這意味着我需要在幾個地方複製解析/顯示代碼/邏輯,這是我試圖避免的。 – stpe 2009-05-18 08:24:13