在Controller中的Actions之间传递数据(ASP.NET MVC)

时间:2017-03-23 15:37:57

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

我需要在COntroller中的Actions之间传递数据

我有这个Action,我将数据写入TempData:

 [HttpPost]
    public ActionResult Index(string question1, string question2, string question3, string question4, string question5, string question6, string question7, string question8, string question9, string question10, Int32 id)
    {
        QuestionBlock question = new QuestionBlock
        {
            Question1 = question1,
            Question2 = question2,
            Question3 = question3,
            Question4 = question4,
            Question5 = question5,
            Question6 = question6,
            Question7 = question7,
            Question8 = question8,
            Question9 = question9,
            Question10 = question10,
            Interview_Id = id,
        };

        TempData["id"] = id;
        db.QuestionBlocks.Add(question);
        db.SaveChanges();
        return Json(new { Result = "Success", Message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
    }

在这个Action中,我需要将TempData传递给变量并将其写入datatable

public ActionResult Update(string start, string end, string link)
    {

        ApplicationDbContext context = new ApplicationDbContext();
        var id = (int?) TempData["id"];
        IEnumerable<InvitationMail> customers = context.InvitationMails
            .Where(c => c.LinkStart == null)
            .Where(c => c.LinkEnd == null)
            .Where(c => c.Link == null)
            .AsEnumerable()
            .Select(c => {
                c.LinkStart = start;
                c.LinkEnd = end;
                c.Link = link;
                c.Interview_Id = id;
                return c;
            });


        foreach (InvitationMail customer in customers)
        {
            // Указать, что запись изменилась
            context.Entry(customer).State = EntityState.Modified;
        }

我在第一个控制器的TempData上设置断点,我看到数据写入TempData

这是屏幕

enter image description here

这是第二个断点

TempData null

屏幕 enter image description here

为什么会这样?

0 个答案:

没有答案