PartialView cumulatively adding issue

北城以北 提交于 2019-12-13 18:12:31

问题


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)

来源:https://stackoverflow.com/questions/17186424/partialview-cumulatively-adding-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!