2011-03-05 156 views
0

我想建立一個幫手來創建一個簡單的單級菜單。在調用菜單幫助程序時,我想使用對象文字符號,以便我可以在視圖中定義菜單項。ASP.Net MVC 3自定義HTML助手 - 如何通過列表<T>幫助使用對象文字符號

public class ActionsMenuHelper 
{ 

    public static string ActionsMenu(IList<ActionsMenuItem> menuItems) 
    { 
     string result = ""; 

     return result; 
    } 


} 

我只是不確定調用菜單的語法。 我試過類似的東西。

@ActionsMenuHelper.ActionsMenu(List<ActionsMenuItem>{ new {Name = "Foo"}, 
                new {Name = "Bar"} 
               }); 

我明顯失去了如何做到這一點。

回答

0

你非常接近。試試這個:

@ActionsMenuHelper.ActionsMenu(new List<ActionsMenuItem> { new ActionsMenuItem { Name = "Foo" }, new ActionsMenuItem { Name = "Bar" }}); 

雖然,如果你有多個項目,可能會有點混亂。我會預先定義的列表可讀性:

@{ 
    var menuItems = new List<ActionsMenuItem> { 
     new ActionsMenuItem { Name = "Foo" }, 
     new ActionsMenuItem { Name = "Bar" }, 
     new ActionsMenuItem { Name = "Etc" } 
    }; 
} 

@ActionsMenuHelper.ActionsMenu(menuItems); 

更多信息,可以發現here

+0

工作就像一個魅力...謝謝 – JBeckton 2011-03-05 17:33:24