简体   繁体   English

Asp.net Core Mvc6 临时数据列表<string>一片空白</string>

[英]Asp.net Core Mvc6 TempData List<string> is null

I am doing an Asp.Net Core MVC6 App.我正在做一个 Asp.Net Core MVC6 应用程序。

I am using TempData to use it from the View我正在使用 TempData 从视图中使用它

I am using like this.我正在这样使用。

private async void CreateClaimsByUserRole(string role, string usertType)
        {
            List<string> permission = await _iUIConfig.CreateClaimsByUserRole(role, usertType);
            TempData["MyList"] = permission;
            TempData.Keep();
        }

I am saving a List<string>我正在保存一个List<string>

Here is some other functions这是一些其他功能

  public async Task<List<string>> CreateClaimsByUserRole(string role, string usertType)
        {
            List<RolesAccessModel>? oResponse = await GetRolesPerApplication();
            List<string> permissions = TransformRowsIntoPermissions(oResponse,role, usertType);
            return permissions;
        }

And

private List<string> TransformRowsIntoPermissions(List<RolesAccessModel>? rows, string role, string usertType)
        {
             List<string> permissionList = new();
            if(rows!=null)
            { 
                foreach (RolesAccessModel row in rows)
                {
                    if (row.Roles!=string.Empty && row.Roles != null && !row.Roles.Contains(role))
                        continue;
                    if (row.UserType != string.Empty && row.UserType != null && !row.UserType.Contains(usertType))
                        continue;

                       // if we hget here we have a match
                       if (!permissionList.Contains(row.EventName))
                        permissionList.Add(row.EventName);
                }
            }
            return permissionList;
        }

As it says here正如这里所说

I can do this in the same Method and works fine..我可以用相同的方法做到这一点并且工作正常..

List<string> SomeList = TempData["MyList"] as List<string>;

But if I want to retrieve the data in another Method, It is null..但是如果我想在另一个方法中检索数据,它是空的..

The only way to retrieve data is using检索数据的唯一方法是使用

var SomeList = TempData["MyList"] ;

I need to retrieve the data from the View, I have the same problem我需要从视图中检索数据,我有同样的问题

 @if (TempData["Claims"] != null)
    {
        var claims = TempData["MyList"] as List<string>;
       
            @foreach (string permission in claims)
            {
                <p>@permission</p>
            }
    }

Where var claims = TempData["MyList"] as List<string> ;其中var claims = TempData["MyList"] as List<string> ; is null一片空白

Reading this page, I also add in Program.cs阅读页面,我还添加了 Program.cs

builder.Services.Configure<CookieTempDataProviderOptions>(options => {
    options.Cookie.IsEssential = true;
});

But still does not work.但还是不行。

What I am missing?我缺少什么?

Thanks谢谢

It was related a serialize/deserialize issue,If you are not trying with simple types,I recomand you to serialize it before adding it to the dictionary它与序列化/反序列化问题有关,如果您不尝试使用简单类型,我建议您在将其添加到字典之前对其进行序列化

If you try as below:如果您尝试如下:

List<decimal> strlist = new List<decimal> { 1, 2, 3 };
TempData["SomeList"] = strlist;

You would got an error:你会得到一个错误:

在此处输入图像描述

It indicates your TempDataDictionary would be serialized before appended to cookie它表示您的 TempDataDictionary 在附加到 cookie 之前将被序列化

For Complex types, you could try as below:对于复杂类型,您可以尝试如下:

 List<string> strlist = new List<string> { "q", "w", "e" };
 var jsonstr= JsonSerializer.Serialize(strlist);                
 TempData["SomeList2"] = jsonstr; 

deserialize:反序列化:

var tempobj2 = TempData["SomeList2"];
var list = JsonSerializer.Deserialize<List<string>>(tempobj2 as string);

and You could cast the value to string[] indicates it was serialized/deserialized like below:并且您可以将该值转换为 string[] 表示它已被序列化/反序列化,如下所示:

List<string> strlist = new List<string> { "q", "w", "e" };
var jsonStr= JsonSerializer.Serialize(strlist);
var obj = JsonSerializer.Deserialize<string[]>(jsonStr) as object;

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM