0

複数のタイプのページを編集できるセクションの編集画面で、cms を構築しています。URL は次のように自然なままにする必要があります。

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

このシナリオでは、Get と Post の両方に Index アクションが使用されます。

現時点では、すべてのページ タイプで必要なすべてのデータを含む 1 つの大規模な ViewModel があります。

これはかなり間違っていると感じており、投稿のページ更新の種類を決定するための醜い解決策になります。

アクションを同じに保ちながら、異なる厳密な型の 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

すべてのページ タイプが同じアクションにポスト バックされ、コンテンツなどを更新します。

あなたの問題があります。同じアクションですべてのポスト バックを処理する必要はありません。機能 (コンテンツ、ギャラリー、ブログ) ごとに 1 つのコントローラーを作成します。これが 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 に答える