-->

PartialView cumulatively adding issue

2019-07-24 17:36发布

问题:

I want to make a modular web site.Everything is going well but i have encountered a problem when cumulatively added module.I want to load content dynamically as below:

For contact page:

  1. Content.cshtml : Lorem ipsum dolor
  2. Contact.cshtml
  3. Content.cshtml : Adress : Tbilisi/Georgia

But load only first content rightly as below

  1. Content.cshtml : Lorem ipsum dolor
  2. Contact.cshtml
  3. Content.cshtml : Lorem ipsum dolor

How can i solve this problem?

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; }
}

Controller

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)