2009-10-05 39 views

回答

0

絕對答案。

0

不是100%,但我不認爲這是可能的。您想從Partial的ViewPage中引用什麼? 難道你不能在ViewPage和ViewUserControl之間共享一個模型嗎?

0

似乎有這個沒有標準的屬性,因此你應該通過的ViewPage對象,以局部視圖自己:你需要使用的ViewData或模型共享NO

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

我的解決方案是部分控件使用的任何模型的基類。 當你需要指定一個模型,但希望局部視圖能夠訪問來自包含View的模型的特定事物時,它很有用。

注意:此解決方案將自動支持部分視圖的層次結構。

用法:

當你打電話的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; 
      } 
     } 
    } 
} 
相關問題