2009-10-05 3 views

답변

0

절대 대답은 NO

당신은 그것을 공유 할을 ViewData 또는 모델을 사용할 필요를 .

0

100 % 아니지만 가능하다고 생각합니다. 구체적으로 Partial에서 ViewPage로 참조하고 싶은 것은 무엇입니까? ViewPage와 ViewUserControl간에 모델을 공유 할 수 없습니까?

0

이에 대한 표준 속성이 그래서 당신이 부분도 자신에게 ViewPage 개체를 통과해야이없는 것 같다

<% Html.RenderPartial("partial_view_name", this); %> 
0

내 솔루션은 부분 컨트롤로 사용되는 모든 모델의 기본 클래스입니다. 모델을 지정해야하지만 부분보기가 포함 된보기의 모델에서 특정 항목에 액세스 할 수 있도록해야하는 경우에 유용합니다.

참고 :이 솔루션은 부분 뷰의 계층 구조를 자동으로 지원합니다.

사용법 :

당신이 RenderPartial 전화합니다 (뷰) 모델을 제공합니다. 개인적으로 나는 patial view가 부모 모델에서 필요로하는 것이 무엇이든간에 구성된 페이지상의 뷰를 생성하는이 패턴을 선호합니다.

현재 모델에서 ProductListModel을 만듭니다. 그러면 부모 모델을 부분 뷰에 쉽게 사용할 수 있습니다.

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

는 부분 컨트롤 자체에서는 강력한 형식의 뷰와 ProductListModel를 지정합니다. 내가 다시 들어보기로 부분에서 커플 링을 방지하기 위해 모델을 지정 IShoppingCartModel을 사용하고 있습니다 : 부분보기

참고

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

모델 클래스입니다.

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

     } 

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

    } 

BASECLASSES :

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; 
      } 
     } 
    } 
} 
관련 문제