【问题标题】:In MVC 4 How do I Add multiple Collections to a session?在 MVC 4 中,如何将多个集合添加到会话中?
【发布时间】:2013-12-24 01:17:10
【问题描述】:

我正在使用 MVC 4 和 Razor 语法来创建一个基于使用脚手架(基于数据库的开发)创建的类的集合,我可以将第一个集合添加到 Session 并将其返回到索引视图并显示它在页面上。

当我尝试向会话变量添加第二个集合时,它给了我一个错误。

 Unable to cast object of type
'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type
'EagleEye.Models.tblTask'.

我做错了什么 - 如何将 2 个集合添加到会话中?!

Index.cshtml(使用 Razor 语法的我的索引视图)

@model List<myApp.Models.tblTask>
<table>
  @{

                foreach (var tblTask in Model)
                {

                        <tr>
                        <td>
                            TaskName: @tblTask.Name
                        </td>

                        <td>
                            Desc: @tblTask.Description
                        </td>

                        <td>
                            Schedule: @tblTask.Freq @tblTask.FreqUnit
                        </td>

                        <td>
                            Reocurring?: @tblTask.ReocurringTask.ToString()
                        </td>
                    </tr>       
                }      
            }
    </table>

这是我的 HomeController.cs 中代码的“ActionResult”部分:

    [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'.

我已经为此奋斗了几天并且已经发展了一段时间,但是我才刚刚开始学习在我被难住时提出问题的力量(而不是继续用头撞墙直到某些东西陷入困境(通常是我的头脑),所以如果我的问题没有很好地形成,请告诉我。

谢谢! 丹

【问题讨论】:

  • OK...我终于看到曙光了! ..这是有效的更新代码:

标签: c# asp.net-mvc asp.net-mvc-4 session razor


【解决方案1】:

因为,在您的 if 条件中,您将 Session["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");
}

【讨论】:

  • 谢谢!!现在似乎很明显;-)
【解决方案2】:

我终于看到了曙光——注意 HomeController.cs 中的变化 "TaskCollection = (List)Session["TaskCollection"]; "

    [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");
    }

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多