1

我想做一个模块化的网站。一切都很顺利,但是在累积添加模块时遇到了问题。我想动态加载内容,如下所示:

对于联系页面:

  1. Content.cshtml : Lorem ipsum dolor
  2. 联系人.cshtml
  3. Content.cshtml:地址:第比利斯/格鲁吉亚

但仅正确加载第一个内容,如下所示

  1. Content.cshtml : Lorem ipsum dolor
  2. 联系人.cshtml
  3. Content.cshtml : Lorem ipsum dolor

我怎么解决这个问题?

tbl模块

+---------+-------------------------+
| ModulId |  PhysicalPath           |
+---------+-------------------------+
|    1    |  /Content.cshtml        |
+---------+-------------------------+
|    2    |  /Contact.cshtml        |
+---------+-------------------------+

页面

+---------+-------------------------+
| PageId  |  Page                   |
+---------+-------------------------+
|    1    |  About                  |
+---------+-------------------------+
|    2    |  Contact                |
+---------+-------------------------+

表内容

+------------+----------------------------+
| ContentId  |  Content                   |
+------------+----------------------------+
|    1       |  Lorem ipsun dolor...      |
+------------+----------------------------+
|    2       |  Adress : Tbilisi/Georgia  |
+------------+----------------------------+

tblPageModules

+---------+-------------+-------------+
| PageId  |  ModuleId   |  OrderId    |
+---------+-------------+-------------+
|    2    |      1      |      1      |
+---------+-------------+-------------+
|    2    |      2      |      2      |
+---------+-------------+-------------+
|    2    |      1      |      3      |
+---------+-------------+-------------+ 

模块存储库.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);
    }
}

页面.cshtml

@model ModuleRepository
@foreach (var modul in Model.Module)
{
    @Html.Partial(@modul.PhysicalPath, Model)
}

内容.cshtml

@model ModuleRepository
@Html.Raw(@Model.Content.Content)
4

0 回答 0