【发布时间】:2014-06-30 23:59:22
【问题描述】:
问题陈述:
我正在尝试为 MVC 控制器中的会话对象分配一个值,它给出了异常,因为 对象引用未设置为对象的实例。
我有两个控制器
- 主控制器
- 辅助控制器
当我在主控制器中为会话分配值时,它工作正常。但如果我在辅助控制器的 Test() 方法中分配相同的值,则会出错。
我在这里做错了什么???
主控制器:
public class MainController: Controller
{
SecondaryController secCont=new SecondaryController();
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
Session["LogID"] = 10;
//This is working fine.
//Instead of this i want call secCont.Test(); method where value for session assigned, it is giving error.
}
return View(loginModel);
}
}
二级控制器:
public class SecondaryController: Controller
{
public void Test()
{
Session["LogID"] = 10;
// Giving Error as **Object reference not set to an instance of an object.**
}
}
【问题讨论】:
标签: c# .net asp.net-mvc session controller