【问题标题】:Set TempData in ViewComponent在 ViewComponent 中设置 TempData
【发布时间】:2020-06-16 11:48:38
【问题描述】:

我想在 View Component 中设置 TempData,这样当 View Component 出现错误时,我可以在 Login Action 中检查它。

这是我的 .ts 文件代码

window.onload = function () {

    fetch('../Controller/ActionName',
        {
            headers: {
                RequestVerificationToken: (<HTMLInputElement>document.getElementById("Token")).value
            }
        })
        .then((response) => {
            if (response.status != 200) {
                redirectToHomePage();
            }
            response.text().then((data) => {
                document.getElementById("Id")!.innerHTML = data;
            });
        });

    // Redirect to home page
    function redirectToHomePage() {
        var url = window.location.origin;
        window.location.href = url + '/Login/Login/';
    }
};

下面是 ViewComponent 代码

public IViewComponentResult TestViewComponent()
{
    try
    {
        // Do code here
    }
    Catch(Exception ex){                
         TempData["Error"] = "Err msg";
         return View(viewName)
    }

    return View(viewName);
}

在 ViewComponent 中正确设置了 TempData,但是当从 .ts 文件从 redirectToHomePage() 函数返回到 Login/Login TempData 的响应将为空。

那么如何在登录操作中获取 TempData

提前谢谢你

【问题讨论】:

标签: asp.net-core


【解决方案1】:

这里是a definition of ViewComponent.TempData,并且在链接中你可以看到TempData只有get方法,所以你不能将TempData传递给Login/Login

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多