【问题标题】:TempData working with a delay and is null for the first timeTempData 延迟工作,第一次为空
【发布时间】:2021-02-16 11:57:06
【问题描述】:

在 Asp.net MVC 中:第一次重定向的 TempData 为空,在第二次重定向中有效,有时效果很好,有时获得第三次重定向 需要多次刷新页面才能看到TempData的效果 它在本地主机上运行良好,但在服务器上无法正常运行。
例如:
在下面这段代码中的“VerifyPaymentView”动作tempdata[“VerifyInfo”]第一次为null,需要多次刷新页面才能得到tempdata的内容

public async Task<ActionResult> Verify(int id)
    {
        var verifyInfo = await VerifyPayment(id, new List<Func<HttpRequestBase, Presenter, PayData, Task<VerifyCallbackData>>>()
        {
            SetPackage,
            SetExam,
            SetQuestionBankId,
            SetQuestionBankGroupId,
            SetReportCardLink,
            VerifySpace
        });

        if (verifyInfo == null)
        {
            TempData["Error"] = true;
            return RedirectToAction("VerifyPaymentView");
        }

        TempData["VerifyInfo"] = verifyInfo;

        return RedirectToAction("VerifyPaymentView");
    }

 public ActionResult VerifyPaymentView()
    {
        var userId = User.GetUserId();
        var isLogin = User.Identity.IsAuthenticated;

        if (TempData["Error"] != null)
        {
            TempData["Error"] = true;
            return View();
        }

        if (TempData["VerifyInfo"] != null)
        {
            var verifyInfo = (VerifyInfo)TempData["VerifyInfo"];
            return View(verifyInfo);
        }
        return null;
    }

【问题讨论】:

    标签: asp.net-mvc tempdata


    【解决方案1】:

    在 VerifyPaymentView 中使用 TempData.Keep(),这将允许您读取值而不将其标记为删除。

    @{
        TempData.Keep("VerifyInfo");
    }
    

    【讨论】:

    • 问题是读取后没有保留值,问题是我需要刷新VerifyPaymentView页面才能获取TempData值,大多数时候第一次在服务器上不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多