0

我正在构建一个 cms,在一个部分的编辑屏幕上,您可以编辑多种类型的页面,网址需要保持自然,如下所示:

foob
​​ar.com/edit/section/my-content-page-name foobar.com/edit/section/my-gallery-page-name
foobar.com/edit/section/my-blog-page-name

在这种情况下,索引操作用于获取和发布。

目前我有一个庞大的 ViewModel,它包含所有页面类型所需的所有数据。

我觉得这是非常错误的,并且为决定帖子上的页面更新类型提供了一个丑陋的解决方案。

如何保持 Action 不变,但将其与不同的强类型 ViewModel 一起使用?

这甚至可能吗?

public ActionResult Index(string page)
    {
        var model = _pageManager.GetSection(page, SelectedSite);
        return View(model.PageType, model);

        // renders appropriate View based on page type.

    }

    [Transaction]
    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Index(SectionIndexViewModel model)
    {
        // all page types post back to same action to update content etc.
        // at this point SectionIndexViewModel is getting bloated with properties because it must cater for ALL page types data.

        var action = Request["action"] ?? "";

        // currently use this to determine what event has been triggered 
        switch (action.ToLower())
        {
         // then goes to update the appropriate page, blog or gallery
         // etc.
4

2 回答 2

0

所有页面类型都发回相同的操作以更新内容等。

有你的问题。相同的操作不应该处理所有的回传。为每个功能(内容、图库、博客)创建一个控制器。这就是 MVC 的用途。

单一责任原则也适用于控制者。

您甚至可以将控制器移动到类库中,为您的 CMS 获取类似架构的插件。我在这里描述了如何:http: //blog.gauffin.org/2012/05/griffin-mvccontrib-the-plugin-system/

于 2012-08-21T12:41:04.857 回答
0

我设法通过一些我忘记的 MVC 基础知识来实现​​这一点。

路由保持默认设置。

对于每种 ViewModel 类型,我在表单中提供了一个额外的隐藏字段,其中包含页面/内容/ViewModel 的类型,例如:内容页面或博客页面等。

在 Post 操作中,我从这个隐藏字段中检查页面的类型。

然后使用TryUpdateModel该页面类型的预期 ViewModel 类型。

其余的都是直截了当的。

真的很基本的东西。

于 2012-08-30T04:54:43.107 回答