我想在MVC.NET分層菜單由XML
來創建分層基於XML的菜單<?xml version="1.0" encoding="utf-8" ?>
<NavigationMenu id="1" Name="myMenu" Lang="EN">
<NavigationMenuGroup Header="Home" Name="header1" Link="/home" />
<NavigationMenuGroup Header="Manage" Name="header2" Link="/options" />
<NavigationMenuGroup Header="About" Name="header3" Link="/About" >
<NavigationMenuItem Header="Test1" Name="header5" Link="/page1" />
<NavigationMenuItem Header="Test2" Name="header6" Link="/page2" />
</NavigationMenuGroup>
</NavigationMenu>
我泛爲母版渲染部分菜單(NavMenu.ascx)的帶動下,數據將通過自定義基本控制器提供給主頁面。
NavMenu.ascx將生成類似於以下的代碼(易於插入jQuery或模擬器)。
<ul id="menu"> <li> <h2><a href="/home">Home</a></h2> </li> <li> <h2><a href="/options">Manage</a></h2> </li> <li> <h2><a href="/about">About</a></h2> <li> <h2><a href="/page2">Test1</a></h2> </li> <li> <h2><a href="/page1">Test2</a></h2> </li> </li> </ul>
我也有設置一組類反序列化。
namespace MyMVC.Helpers
{
public class XmlSerializerHelper<T>
{
public Type _type;
public XmlSerializerHelper()
{
_type = typeof(T);
}
public void Save(string path, object obj)
{
using (TextWriter textWriter = new StreamWriter(path))
{
XmlSerializer serializer = new XmlSerializer(_type);
serializer.Serialize(textWriter, obj);
}
}
public T Read(string path)
{
T result;
using (TextReader textReader = new StreamReader(path))
{
XmlSerializer deserializer = new XmlSerializer(_type);
result = (T)deserializer.Deserialize(textReader);
}
return result;
}
}
}
我不確定什麼是最好的方式做到這一點,而不打破MVC校長?
是在你的菜單中的每個項目實際上是一個頭? – 2010-06-19 16:07:09