【问题标题】:How to organize views and controllers hierarchically when having model with many children有许多孩子的模型时如何分层组织视图和控制器
【发布时间】:2009-03-27 09:36:40
【问题描述】:

假设我有一个列出 Persons 的网站,每个 Person 都有多个属性,一些是一对一的(姓名、地址),一些是一对多的(喜欢的颜色、银行账户等)。

在我的业务层中,它的层次结构很好。

如何组织我的控制器和视图? 我应该有 PersonsController、FavoriteColorsController 等吗?都驻留在同一个命名空间和文件夹中?或者我应该只有一个具有许多操作的 PersonsController,例如 IndexFavouriteColors、UpdateFavoriteColor 等。这两个选项都不完全正确。第一个没有表明 FavoriteColors 是 Person 的子级,并且只能在人的上下文中使用。第二个将创建一个巨大的 PersonController。

当然,观点也是如此。最好有

  • 视图/人员/index.aspx
  • Views/Persons/details.aspx
  • 视图/人/等
  • 视图/人物/FavoriteColors/index.aspx
  • Views/Persons/FavoriteColors/details.aspx

在这个例子中,我只为 Person 提供了几个一对多的属性,但实际上有很多 (10+),因此需要更加清晰。

谢谢。

【问题讨论】:

    标签: asp.net-mvc project-organization


    【解决方案1】:

    其实我在“Areas”中找到了答案,据说 Rails 支持,但 MVC 不支持,虽然有私有实现:

    Grouping Controllers with ASP.NET MVC
    Creating MVC "Areas" as Subfolders under Views

    【讨论】:

    • 第二个链接不知何故被破坏了。似乎有一个额外的尾部斜杠。
    【解决方案2】:
    PersonsController.cs
    
    public ActionResult Index()
    {
      return View();
    }
    public ActionResult Details()
    {
      return View();
    }
    public ActionResult Info()
    {
      return View();
    }
    

    ....

    右键单击每个方法并生成视图

    在 Views 文件夹中你应该有一个结果

    Views
    ----- Persons
    -------------Index
    -------------Details
    -------------Info
    -------------....
    

    【讨论】:

    • 人本身不是问题。它是当前视图的“子”视图的划分。我想创建一个表示路由结构的视图文件夹结构,例如: /Person/ /Person/{id}/details /Person/{id}/FavoriteColors/ /Person/{id}/FavoriteColors/{id}/details
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多