1

部分ビューから部分ビューを含むViewPageの参照を取得する方法はありますか?

4

4 に答える 4

0

絶対的な答え:いいえ

共有するには、ViewDataまたはModelを使用する必要があります。

于 2009-10-07T16:44:18.990 に答える
0

これには標準のプロパティがないようです。そのため、ViewPageオブジェクトを渡して自分で部分的に表示する必要があります。

<% Html.RenderPartial("partial_view_name", this); %>
于 2009-10-07T05:18:20.123 に答える
0

100%ではありませんが、これは不可能だと思います。パーシャルからViewPageで具体的に何を参照しますか?ViewPageとViewUserControlの間でモデルを共有するだけではいけませんか?

于 2009-10-06T17:52:34.067 に答える
0

私のソリューションは、部分的なコントロールで使用されるすべてのモデルのベースクラスでした。モデルを指定する必要があるが、部分ビューが含まれているビューのモデルから特定のものにアクセスできるようにしたい場合に便利です。

注:このソリューションは、部分ビューの階層を自動的にサポートします。

使用法:

RenderPartialを呼び出すときは、モデルを提供します(ビュー用)。個人的には、このパターンが好きです。これは、親モデルからの空間ビューに必要なもので構成されるビューをページ上に配置することです。

現在のモデルからを作成しますProductListModel。これにより、親モデルを部分ビューで簡単に使用できるようになります。

  <% Html.RenderPartial("ProductList", new ProductListModel(Model) 
                       { Products = Model.FilterProducts(category) }); %> 

部分コントロール自体でProductListModel、を強く型付けされたビューとして指定します。

<%@ Control Language="C#" CodeBehind="ProductList.ascx.cs"
    Inherits="System.Web.Mvc.ViewUserControl<ProductListModel>" %>

部分ビューのモデルクラス

注:IShoppingCartModel部分的なビューから含まれているビューへの結合を回避するために、モデルを指定するために使用しています。

public class ProductListModel : ShoppingCartUserControlModel
    {
        public ProductListModel(IShoppingCartModel parentModel)
            : base(parentModel)
        {

        }

        // model data 
        public IEnumerable<Product> Products { get; set; }

    }

ベースクラス:

namespace RR_MVC.Models
{
    /// <summary>
    /// Generic model for user controls that exposes 'ParentModel' to the model of the ViewUserControl
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ViewUserControlModel<T>
    {
        public ViewUserControlModel(T parentModel)
            : base()
        {
            ParentModel = parentModel;
        }

        /// <summary>
        /// Reference to parent model
        /// </summary>
        public T ParentModel { get; private set; }
    }

    /// <summary>
    /// Specific model for a ViewUserControl used in the 'store' area of the MVC project
    /// Exposes a 'ShoppingCart' property to the user control that is controlled by the 
    /// parent view's model
    /// </summary>
    public class ShoppingCartUserControlModel : ViewUserControlModel<IShoppingCartModel>
    {
        public ShoppingCartUserControlModel(IShoppingCartModel parentModel) : base(parentModel)
        {

        }

        /// <shes reummary>
        /// Get shopping cart from parent page model.
        /// This is a convenience helper property which justifies the creation of this class!
        /// </summary>
        public ShoppingCart ShoppingCart
        {
            get
            {
                return ParentModel.ShoppingCart;
            }
        }
    }
}
于 2009-10-13T00:12:07.303 に答える