我能找到解決方案,雖然它不是很漂亮。它使用一點反射和硬編碼映射到對象樹。希望這對於任何需要MVC中的ViewState的人來說都是一個很好的起點。
基本上它涉及到反序列化ViewState到一個對象,然後使用反射,調用控件的LoadViewState與對象的樹中的右分支。
string viewState = Request.Form["__VIEWSTATE"];
if (!string.IsNullOrEmpty(viewState))
{
LosFormatter formatter = new LosFormatter();
object savedStateObject = formatter.Deserialize(viewState);
MethodInfo method = grid.GetType().GetMethod("LoadViewState", BindingFlags.NonPublic | BindingFlags.Instance);
// TODO: Find a less brittle/more elegant way to isolate the appropiate viewstate object for this control
// In the case of Telerik's RadGrid, the key wasy find the tree that had an array of 13 objects
method.Invoke(grid, new object[] { (((((((((savedStateObject as Pair).First as Pair).Second as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).Second as System.Collections.ArrayList)[1] as Pair).First });
}
string eventArgument = Request.Form["__EVENTARGUMENT"];
if (!string.IsNullOrEmpty(eventArgument))
{
grid.RaisePostBackEvent(eventArgument);
}
看到這個職位的詳細信息:Supporting ViewState in an MVC ViewUserControl
所以......存在這種產品,會做你想要什麼的MVC版本。但是你想破解WebForms版本,並將其轉向你的意願呢? –
是的,MVC版本沒有我需要的所有東西,比如多行選擇和許多其他的東西。我真的需要得到的AJAX版本工作:/ – Levitikon
也許這可以與多行選擇工作? (http://demos.telerik.com/aspnet-mvc/grid/checkboxesajax)。不知道許多其他事情是什麼,但至少在正確的方向邁出了一步:) – carlbergenhem