English 中文(简体)
当我事先不知道物体时,我怎么能够建立起一个能够建造MvcHtmlString Breadcrumb的修复功能?
原标题:How can I build a recursive function that builds an MvcHtmlString Breadcrumb trail when I don t know the object beforehand?

我认为,我需要使用一种利用玛尔布达语来实现这一目标的“html”助手方法。

我正在考虑采取这样的行动:

    public static MvcHtmlString GetCategoryBreadCrumbs<T>(
        this HtmlHelper html,
        IEnumerable<T> currentCat,
        Func<T, T> parentProperty,
        Func<T, string> itemContent)
    {
        var sb = new StringBuilder();
        sb.AppendLine(itemContent(currentCat));
        if (currentCat.parentProperty.Count() > 0)
            sb.AppendLine(GetCategoryBreadCrumbs(html, currentCat.parentProperty, itemContent);

        return MvcHtmlString.Create(sb.ToString());
    }

因此,我要说的是:

<%: Html.GetCategoryBreadCrumbs(
Model, l => l.parentCategories, 
    l => l.catID, 
    l => l.catName)%>

显然,即使是在假装编码一级,我前面的内容也是可怕的。

Lamba/Genericmeth如何工作?

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 我认为,你应考虑将“类别”的复读物与创造面包舱分开。 如同你所做的那样,将两者结合起来,使守则更加难以理解和维护。 通过将可能发生变化的执行部分分开,你使最终结果更容易理解、不那么脆弱和不太容易。

public static IEnumerable<T> GetHierarchy<T>( this T item, Func<T,T> getParent )
{
    while( item != null )
    {
        yield return item;
        item = getParent( item );
    }        
}

现在,你可以使用<代码>String.Join(来做其他工作:

<%: MvcHtmlString.Create(
      string.Join( " >> ", Model.GetHierarchy( item => item.GetParent() )
                                .Select( item => item.catName ) ) %>

在上述法典中,我假定,>提供了一种简单的手段,可以与特定物体的父母联系。 如果该法典在操作时间上有所不同,则你可以使用另一种手法,或建立接口,统一具有“父母”的不同类别。

显然,如果你再次以多种观点制造面包.,那么你就应考虑在帮助者中总结这一点。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签