2014-10-29 156 views
0

我在c#中有三個控制器類。說AController,BController,CController。 我曾經使用AController設置BController的私有成員的值。從控制器從另一個控制器在c#中的值#

public BController{ 
string test = ""; 
public BController(string input) 
{ 
this.test = input; 
} 
} 

如何從CController訪問測試。

的任何方法而不是分配成員作爲靜態變量

+0

也許,你錯誤的邏輯?你能提供更多的代碼和一些關於你的需求的更多信息嗎? – noobed 2014-10-29 14:21:04

+0

[反映控制器列表](http://stackoverflow.com/questions/3680609/reflect-over-list-of-controllers) – Alexander 2014-10-29 14:21:56

回答

1

您可以返回重定向到CControllerAction。

public class BController() 
{ 
    public ActionResult BControllerAction() 
    { 
     return RedirectToAction("CControllerAction", "CController", new { param = "some string" }) 
    } 
} 

public class CController() 
{ 
    public ActionResult CControllerAction(string param) 
    { 
     if(!String.IsNullOrEmpty(param)) 
     { 
      //do smth 
     } 
    } 
} 
0

您可以獲取和使用屬性模式

public string TestB { 
    get {return this.test;} 
    set {this.test = value;} 
} 
0

你的問題沒有指定要使用的技術設置一個私有字段的值。因此,我的第一個建議是基於純C#類。

測試屬性是一個私人字段,不能在課程外部訪問。您可以通過屬性公開它。如果要使用當前程序集訪問該字段,可以將該字段設置爲內部。有關更多信息,請參閱Access Modifier。我建議你使用一個屬性,因爲它給你更多的控制權。

private string test; 
public string Test 
{ 
get{return test;} 
set{test=value'} 
} 

如果您使用的是MVC,那麼您可以使用TempData。 TempData是從TempDataDictionary類派生的字典。它用於存儲僅用於以下請求的數據。欲瞭解更多信息,請參閱TempData

相關問題