我是ASP.NET/C#中的MVC2的新手,如果我誤解了某些內容,請原諒我。我有我的控制器中的代碼類似於:如何引用傳遞給View的單個對象的成員?
var singleInstance = new Person("John");
ViewData["myInstance"] = singleInstance;
return View();
所以在我看來,Index.aspx,我希望能夠引用該對象的成員。例如,Person有一個名爲Name的成員,它在構造函數中設置。在視圖中,我想從存儲在ViewData對象中的Person中獲取Person.Name。例如:
<%= ViewData["myInstance"].name %>
這是行不通的。唯一真正的解決方法,我發現是做這樣的事情:
<% var thePerson = ViewData["myInstance"];
print (or whatever the method is) thePerson.Name;
%>
任何幫助,將不勝感激......這是如此PHP/Zend框架更容易... 嘆息