我试图通过我的布局中的控制器使用部分视图,如下所示:
_Layout.cshtml
:
<ul id="menu">
<li>@Html.Action("Menu")</li>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
MenuController.cs
:
public class MenuController : Controller
{
//
// GET: /Menu/
public ActionResult Index()
{
var menu = new Models.Menu()
{
Items = new List<Models.MenuItem>()
{
new Models.BaseMenuBtn() { Label = "One", Link= "Home", CssClass="a"},
new Models.BaseMenuBtn() { Label = "One", Link= "Home", CssClass="b"}
}
};
return View();
}
}
型号:
Menu.cs
:
public class Menu : MenuItem
{
public List<MenuItem> Items { get; set; }
public override string Render()
{
StringBuilder sb = new StringBuilder("<div>");
foreach (var item in Items)
{
sb.Append(item.Render());
}
sb.Append("</div>");
return sb.ToString();
}
public Menu()
{
Items = new List<Models.MenuItem>()
{
new Models.BaseMenuBtn() { Label = "One", Link= "Home", CssClass="a"},
new Models.BaseMenuBtn() { Label = "One", Link= "Home", CssClass="b"}
};
}
}
MenuItem.cs
:
public abstract class MenuItem
{
public string CssClass { get; set; }
public abstract string Render();
}
BaseMenuBtn.cs
public class BaseMenuBtn : MenuItem
{
public string Label { get; set; }
public string Link { get; set; }
public override string Render()
{
return string.Format("<div href='{0}' class='{1}'> {2} </div>", this.Link , this.CssClass, this.Label);
}
}
而且,从我在布局中添加的行中,我收到了这个错误:
执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。
请帮忙。