我想制作一个模块化的网站。一切顺利但我在累积添加模块时遇到了问题。我想动态加载内容如下:
联系页面:
但是只加载第一个内容,如下所示
我该如何解决这个问题?
tblModule
+---------+-------------------------+
| ModulId | PhysicalPath |
+---------+-------------------------+
| 1 | /Content.cshtml |
+---------+-------------------------+
| 2 | /Contact.cshtml |
+---------+-------------------------+
的tblpage
+---------+-------------------------+
| PageId | Page |
+---------+-------------------------+
| 1 | About |
+---------+-------------------------+
| 2 | Contact |
+---------+-------------------------+
tblContent
+------------+----------------------------+
| ContentId | Content |
+------------+----------------------------+
| 1 | Lorem ipsun dolor... |
+------------+----------------------------+
| 2 | Adress : Tbilisi/Georgia |
+------------+----------------------------+
tblPageModules
+---------+-------------+-------------+
| PageId | ModuleId | OrderId |
+---------+-------------+-------------+
| 2 | 1 | 1 |
+---------+-------------+-------------+
| 2 | 2 | 2 |
+---------+-------------+-------------+
| 2 | 1 | 3 |
+---------+-------------+-------------+
ModuleRepository.cs
public class ModuleRepository
{
public IEnumerable<Modules> Module { get; set; }
public IEnumerable<PageModules> PageModule { get; set; }
public ContentBlock Content { get; set; }
}
控制器
public ActionResult Page()
{
int PageId = default(int);
if (RouteData.Values["id"] != null)
{
PageId = int.Parse(RouteData.Values["id"].ToString());
}
using (ContentModel db = new ContentModel())
{
var module = from n in db.PageModule
join m in db.Module on n.ModuleId equals m.ModuleId
where n.PageId == PageId
orderby n.OrderId
select m;
var model = new ModuleRepository
{
Module = ViewName.ToList(),
Content = db.ContentBlock.Where(n => n.PageId == PageId).First()
};
return View(model);
}
}
Page.cshtml
@model ModuleRepository
@foreach (var modul in Model.Module)
{
@Html.Partial(@modul.PhysicalPath, Model)
}
Content.cshtml
@model ModuleRepository
@Html.Raw(@Model.Content.Content)