I have a following View Method in an ASP.NET MVC 3 Controller that retrieves data from Amazon SimpleDb, stores it in a list and then stores that list object in a session. But at the line where I am storing the userBox object in a session (Session["userBox"] = userBox), I am getting a NullReferenceException. I am sure that userBox is not null. Even if I try to store a simple string in a session (like Session["userBox"] = "test") I still get NullReferenceException.
Here is the code:
public ActionResult SetSidebarAccountBoxSessions(string id)
{
string selectExpression = "select * from MySimpleDBDomain where itemName()='" + id + "'";
SelectRequest sreq = new SelectRequest().WithSelectExpression(selectExpression);
SelectResponse sres = sdb.Select(sreq);
List<User> userBox = new List<User>();
if (sres.IsSetSelectResult())
{
SelectResult selectresult = sres.SelectResult;
foreach (Item item in selectresult.Item)
{
string a = item.Name;
userBox.Add(new User
{
imageThug = item.Attribute[0].Value,
name = item.Attribute[3].V开发者_C百科alue,
bio = item.Attribute[1].Value
});
}
}
Session["userBox"] = userBox;
return View();
}
I am calling this SetSideBarAccountBoxSessions(id) method from another controller method:
HomeController hc = new HomeController();
hc.SetSidebarAccountBoxSessions(item.Name);
Can this be the problem? Please help.
I think this problem is related to the fact that you create HomeController by yourself. You can try to use TransferToRouteResult
to transfer the action to HomeController.
You an find the code of TransferToRouteResult
in this link:
How to simulate Server.Transfer in ASP.NET MVC?
精彩评论