2013-07-30 61 views
-5

我需要從類ControllerContext獲取我的viewmodel。我需要閱讀一些viewmodel ControllerContext的屬性。我怎樣才能做到這一點??從controllerContext獲取viewModel MVC

using System; 
using System.Web; 
using System.Web.Routing; 
using System.Web.WebPages; 

namespace System.Web.Mvc 
{ 
    // Summary: 
    //  Encapsulates information about an HTTP request that matches specified System.Web.Routing.RouteBase 
    //  and System.Web.Mvc.ControllerBase instances. 
    public class ControllerContext 
    { 
     // Summary: 
     //  Initializes a new instance of the System.Web.Mvc.ControllerContext class. 
     public ControllerContext(); 
     // 
     // Summary: 
     //  Initializes a new instance of the System.Web.Mvc.ControllerContext class 
     //  by using the specified controller context. 
     // 
     // Parameters: 
     // controllerContext: 
     //  The controller context. 
     // 
     // Exceptions: 
     // System.ArgumentNullException: 
     //  The controllerContext parameter is null. 
     protected ControllerContext(ControllerContext controllerContext); 
     // 
     // Summary: 
     //  Initializes a new instance of the System.Web.Mvc.ControllerContext class 
     //  by using the specified request context and controller. 
     // 
     // Parameters: 
     // requestContext: 
     //  The request context. 
     // 
     // controller: 
     //  The controller. 
     // 
     // Exceptions: 
     // System.ArgumentNullException: 
     //  One or both parameters are null. 
     public ControllerContext(RequestContext requestContext, ControllerBase controller); 
     // 
     // Summary: 
     //  Initializes a new instance of the System.Web.Mvc.ControllerContext class 
     //  by using the specified HTTP context, URL route data, and controller. 
     // 
     // Parameters: 
     // httpContext: 
     //  The HTTP context. 
     // 
     // routeData: 
     //  The route data. 
     // 
     // controller: 
     //  The controller. 
     public ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller); 

     // Summary: 
     //  Gets or sets the controller. 
     // 
     // Returns: 
     //  The controller. 
     public virtual ControllerBase Controller { get; set; } 
     // 
     // Summary: 
     //  Gets the display mode. 
     // 
     // Returns: 
     //  The display mode. 
     public IDisplayMode DisplayMode { get; set; } 
     // 
     // Summary: 
     //  Gets or sets the HTTP context. 
     // 
     // Returns: 
     //  The HTTP context. 
     public virtual HttpContextBase HttpContext { get; set; } 
     // 
     // Summary: 
     //  Gets a value that indicates whether the associated action method is a child 
     //  action. 
     // 
     // Returns: 
     //  true if the associated action method is a child action; otherwise, false. 
     public virtual bool IsChildAction { get; } 
     // 
     // Summary: 
     //  Gets an object that contains the view context information for the parent 
     //  action method. 
     // 
     // Returns: 
     //  An object that contains the view context information for the parent action 
     //  method. 
     public ViewContext ParentActionViewContext { get; } 
     // 
     // Summary: 
     //  Gets or sets the request context. 
     // 
     // Returns: 
     //  The request context. 
     public RequestContext RequestContext { get; set; } 
     // 
     // Summary: 
     //  Gets or sets the URL route data. 
     // 
     // Returns: 
     //  The URL route data. 
     public virtual RouteData RouteData { get; set; } 
    } 
} 
+0

不完全確定你在這裏問什麼。你想從ControllerContext中訪問另一個類嗎?或者你想訪問ControllerContext的屬性? –

+0

你在說ViewData或ViewModel嗎? – ataravati

+0

隨機旁觀:你沒有使用任何理由/ /和適當的標籤爲您的意見? – Stu

回答

2

嘗試:

ControllerContext.Controller.ViewData.Model 

目前尚不清楚時正是你需要獲取從上下文的數據,但你必須認識到,ViewModel返回到View只有在後可用0事件(在ActionFilter中同樣的方法)。

0

假設我明白的問題,可以用點號訪問ControllerContext的屬性:

ControllerContext.<PropertyName>