我有一个 Controller 操作:
public ActionResult Sub(int id)
{
SubPage_Table subpage_table = db.SubPage_Table.Single(s => s.PageID == id);
if (subpage_table == null)
{
return HttpNotFound();
}
return View(subpage_table);
}
和一个 View :
@model Name.Models.DB.SubPage_Table
@Html.Raw(HttpUtility.HtmlDecode(Model.PageContent))
但是我的布局需要强类型:
@model IEnumerable<Name.Models.DB.SubPage_Table>
@foreach (var item in Model)
{@: <li> @Html.ActionLink((item.PageTitle), "Sub", new { id = item.PageID })</li>
}
如何使用数据填充我的 View 和布局?
最佳答案
以下是更好的选择:
使用数据驱动信息创建部分 View
public LayoutController
{
public ActionResult CreateMenu()
{
var model = db.getmenudata();
return PartialView(model);
}
}
然后您的 Layout/Createmenu.cshtml 仅创建数据驱动的 html。
在您的_Layout.cshtml中:
<html>
... etc
@Html.RenderAction("Createmenu", "layout");
... etc
@RenderBody();
... etc
</html>
关于c# - 数据驱动的asp.net mvc布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23372564/