0
我正在學習MVC,我想了解如何訪問控制器級別的ViewModel。如何訪問控制器上的ViewModel
我的問題是如何訪問MethodTwo中的ViewModel?
- 我是否需要將ViewModel發送回AJAX調用?
- 是否有一種簡單的方法能夠訪問該方法中的視圖模型?
例如:
視圖模型
public class MyViewModel
{
public int MyID { get; set; }
public string MyString { get; set; }
public bool MyBool { get; set; }
}
在控制器我有這樣的:
public ActionResult MethodOne(int myId, string myString, bool myBool)
{
MyViewModel mvm = new MyViewModel { MyID = myId, MyString = myString,yBool = myBool };
return View("MyPrint", mvm);
}
在我MyPrint頁我做一個Ajax GET(相同的控制器),我想訪問相同的ViewModel。
public string MethodTwo()
{
// How can I access the ViewModel here???
//var myString = DoStuff(MyViewModel);
return myString;
}
爲了在'MethodTwo'中訪問模型的值,您需要將它傳遞給'MethodTwo'。如果你正在做一個GET請求,這將是查詢字符串中的值。 ajax請求是什麼樣的? – markpsmith
$阿賈克斯({ 類型: 'GET', 網址: '/主/ MediaReach/GetPrintData', 的contentType: 'text/html的', 數據:{ MRPM:@Model } })。DONE (function(result){(「#abc」)。append(result); print(); })。 – PrivateJoker
@JDS使用ModelBinding技術來獲取Model內的控制器數據。 [好](http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx)教程在這裏。 –