C# 在MVC4中,如何向会话添加多个集合?
我使用MVC4和Razor语法来创建一个集合,该集合基于使用scaffolding(基于数据库的开发)创建的类,我可以将第一个集合添加到会话中,并将其返回到索引视图,并在页面上显示 当我尝试向会话变量添加第二个集合时,它会给我一个错误C# 在MVC4中,如何向会话添加多个集合?,c#,asp.net-mvc,asp.net-mvc-4,session,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Session,Razor,我使用MVC4和Razor语法来创建一个集合,该集合基于使用scaffolding(基于数据库的开发)创建的类,我可以将第一个集合添加到会话中,并将其返回到索引视图,并在页面上显示 当我尝试向会话变量添加第二个集合时,它会给我一个错误 Unable to cast object of type 'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type 'EagleEye.Models.tblTask'. 我做错了
Unable to cast object of type
'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type
'EagleEye.Models.tblTask'.
我做错了什么-如何向会话添加2个集合
Index.cshtml(使用Razor语法的我的索引视图)
我已经为此奋斗了几天,并且已经发展了一段时间,但是我刚刚开始学习当我被难倒时问问题的力量(而不是继续用头撞墙直到有东西(通常是我的头)塌下来,所以如果我的问题没有形成,请让我知道
谢谢!
Dan因为在if条件下,您正在将
会话[“TaskCollection”]
(这是tblTask
的集合)强制转换为tblTask
的单个实例
这应该行得通
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
//Check whether the collection exist in session, If yes read it
// & cast it to the tblTask collection & set it to the TaskCollection variable
if (Session["TaskCollection"] != null)
{
TaskCollection= (List<tblTask>) Session["TaskCollection"];
}
if(newTask!=null)
TaskCollection.Add(newTask);
//Set the updated collection back to the session
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}
[HttpPost]
公共操作结果创建任务(tblTask newTask)
{
var TaskCollection=新列表();
//检查集合是否存在于会话中,如果存在,请读取它
//将其强制转换为tblTask集合(&C)并将其设置为TaskCollection变量
if(会话[“TaskCollection”]!=null)
{
TaskCollection=(列表)会话[“TaskCollection”];
}
if(newTask!=null)
TaskCollection.Add(newTask);
//将更新的集合设置回会话
会话[“TaskCollection”]=TaskCollection;
返回操作(“索引”);
}
我终于看到了曙光——请注意HomeController.cs“TaskCollection=(List)会话[“TaskCollection]”中的更改
[HttpPost]
公共操作结果创建任务(tblTask newTask)
{
var TaskCollection=新列表();
if(会话[“TaskCollection”]!=null)
{
//这是改变的一行--下面的一行是有效的~
TaskCollection=(列表)会话[“TaskCollection”];
}
TaskCollection.Add(newTask);
会话[“TaskCollection”]=TaskCollection;
返回操作(“索引”);
}
好的……我终于看到了曙光!。下面是最新的代码:谢谢!!现在看起来很明显;-)
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
TaskCollection.Add((tblTask)Session["TaskCollection"]);
}
TaskCollection.Add(newTask);
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
TaskCollection = (List<tblTask>)Session["TaskCollection"];
}
return View(TaskCollection);
}
Unable to cast object of type
'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type
'EagleEye.Models.tblTask'.
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
//Check whether the collection exist in session, If yes read it
// & cast it to the tblTask collection & set it to the TaskCollection variable
if (Session["TaskCollection"] != null)
{
TaskCollection= (List<tblTask>) Session["TaskCollection"];
}
if(newTask!=null)
TaskCollection.Add(newTask);
//Set the updated collection back to the session
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
//Here is the line that changed -- the following line works~
TaskCollection = (List<tblTask>)Session["TaskCollection"];
}
TaskCollection.Add(newTask);
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}