【问题标题】:MVC: Design strategy for dynamic content not bound to single pageMVC:动态内容的设计策略不绑定到单页
【发布时间】:2014-09-20 11:21:24
【问题描述】:

我的网站有越来越多的基于特定实体的不同控制器,它们实现接口/存储库模式来填充各种视图。这些视图中的大多数都绑定到单个页面。详细信息、索引等。

此实体数据也将以摘要/压缩形式显示在其他页面上,例如自定义主页和侧边栏(使用布局和部分)。

我不确定使用哪种方法。我有两个想法:

1) 将相关仓库导入到页面模板/侧边栏模板中:

    public HomeController(IMessagesRepository messagesRepository, ICruiseCalendarRepository cruiseCalendarRepository, INewsRepository newsRepository, and so on and so on...)
    //
    {
        this.repoMessages = messagesRepository;
        this.repoCruiseCalendar = cruiseCalendarRepository;
        this repoNews = newsRepository;
        and so on
        and on
        ....
        ....
    }

...并从这些或构建页面。

2) 使用 Html.RenderAction 通过控制器访问动态数据:

<div class="newsBox">
    @{ Html.RenderAction("CustomisedHeadlines", "News");}
</div>
<div class="messageBox">
    @{ Html.RenderAction("LatestMessages", "Messages");}
</div>
<div class="calendarBox">
    @{ Html.RenderAction("WeekItinerary", "CruiseCalendar");}
</div>

欢迎任何关于最佳设计实践的建议。

问候,伙计

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    使用Html.RenderAction 绝对是更好的解决方案。它使控制器简单且可重用。您可以轻松地在视图中添加和删除框,而无需更改控制器。很容易将它们放在不同的视图中。通常它是更灵活的解决方案。

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 2021-05-08
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2011-01-04
      • 2020-06-14
      相关资源
      最近更新 更多