0
我一直在嘗試使用ActionFilterAttribute從數據庫(基於querystring中的「TargetCode」)加載masterpage內容。然而,我有問題的ViewData設置爲從數據庫中返回的數據,在這裏是代碼:ViewData in ActionFilterAttribute - 對象引用未設置爲對象的實例
public override void OnActionExecuting(ActionExecutingContext filterContext)
{ HomeRepository HR =新HomeRepository();
var result = filterContext.Result as ViewResult;
string TargetCode = string.Empty;
Controller control = filterContext.Controller as Controller;
System.Collections.Specialized.NameValueCollection query = filterContext.HttpContext.Request.QueryString;
if (query.Count > 0 && query["TargetCode"] != null && query["TargetCode"].ToString() != "")
TargetCode = query["TargetCode"].ToString();
if (string.IsNullOrEmpty(TargetCode))
if (control != null)
{
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "NoCode" }));
return;
}
if (!hr.CheckTargetCodeExists(TargetCode))
{
if (control != null)
{
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "InvalidCode" }));
return;
}
}
// IF CODE IS VALID, GET THE MASTERPAGE CONTENT AND STORE IT IN VIEWDATA
var ThemeData = hr.GetMasterPageContent(TargetCode);
result.ViewData["ThemeData"] = ThemeData;
}
一切按預期方式工作,除了最後一行(result.ViewData [ 「ThemeData」] = ThemeData)
當調試代碼,它表明ThemeData確實有我從數據庫中獲得的數據,但我無法將其設置爲result.ViewData [「ThemeData」]。錯誤是「對象引用未設置爲對象的實例」。在那條線上。
任何幫助表示讚賞。 非常感謝。
看來「結果」實際上是null,而不是ThemeData。任何人都知道如何獲得結果(ViewResult)來呈現? – 2009-10-14 19:34:36