Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 将对象(和子对象)从Razor页面发送到另一个页面_Asp.net Core_Razor Pages - Fatal编程技术网

Asp.net core 将对象(和子对象)从Razor页面发送到另一个页面

Asp.net core 将对象(和子对象)从Razor页面发送到另一个页面,asp.net-core,razor-pages,Asp.net Core,Razor Pages,我正在尝试将ResumeInfo类型的对象(自定义对象)从一个razor页面发送到另一个razor页面,它工作正常……但我注意到该对象中的所有子对象都为null,它们不应该为null,因为它们具有值 这是我制作对象的类,它有一个其他对象(经验、语言、技能)的列表。当我将对象从一个页面发送到另一个页面时,这些列表返回为空,即使对象有值,因为我在将对象发送到另一个页面之前测试了它们 public class ResumeInfo { public string FullN

我正在尝试将ResumeInfo类型的对象(自定义对象)从一个razor页面发送到另一个razor页面,它工作正常……但我注意到该对象中的所有子对象都为null,它们不应该为null,因为它们具有值
这是我制作对象的类,它有一个其他对象(经验、语言、技能)的列表。当我将对象从一个页面发送到另一个页面时,这些列表返回为空,即使对象有值,因为我在将对象发送到另一个页面之前测试了它们

  public class ResumeInfo
    {
        public string FullName { get; set; }
        public string Profession { get; set; }
        public List<string> Socials { get; set; }
        public List<Skill> Skills { get; set; }
        public List<Language> Languages { get; set; }
        public List<Experience> Experiences { get; set; }
        public string AboutMe { get; set; }

        public List<Language> GetLanguages()
        {
            if (Languages == null)
                Languages = new List<Language>();
            return Languages;
        }
        public List<Skill> GetSkills()
        {
            if (Skills == null)
                Skills = new List<Skill>();
            return Skills;
        }
        public List<Experience> GetExperiences()
        {
            if (Experiences == null)
                Experiences = new List<Experience>();
            return Experiences;
        }
    }
请注意,对象(ResumeInfo)中的其他属性(全名、专业、AboutMe)工作正常,它们有值,只是列表和子对象

这就是我接收物体的方式

    public class GenerateResumeModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public ResumeInfo Info { get; set; }
        public void OnGet()
        {
            //Console.WriteLine(Info.GetExperiences()[0].Name);
        }
    }
为什么没有填充子对象?是否有其他方法发送对象?

检查,我们可以看到对象是路由值

在Asp.net Core Razor页面中,我们不能将复杂对象作为路由数据传递。路由数据功能仅支持简单对象,如
int
string
。如果希望在请求之间保留更复杂的对象,则需要使用
会话
临时数据
(由会话状态支持)

因此,您可以参考以下步骤来启用会话并在请求之间传输复杂对象

  • 在Startup.cs中启用会话中间件:在
    ConfigureServices
    中调用
    AddSession
    ,并在
    Configure
    中调用
    UseSession

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddDistributedMemoryCache();
    
         services.AddSession(options =>
         {
             options.IdleTimeout = TimeSpan.FromMinutes(10); //session expired time
             options.Cookie.HttpOnly = true;
             options.Cookie.IsEssential = true;
         });
    
         services.AddRazorPages();  
     }
    
     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         ... 
         app.UseAuthentication();
         app.UseAuthorization();
         app.UseSession();
         app.UseEndpoints(endpoints =>
         {
             endpoints.MapRazorPages();
         });
     }
    
  • 添加会话或临时数据扩展以存储对象

     //required
     //using Microsoft.AspNetCore.Http;
     //using Newtonsoft.Json;
     public static class SessionExtensions
     {
         public static void Set<T>(this ISession session, string key, T value)
         {
             session.SetString(key, JsonConvert.SerializeObject(value));
         }
    
         public static T Get<T>(this ISession session, string key)
         {
             var value = session.GetString(key);
             return value == null ? default : JsonConvert.DeserializeObject<T>(value);
         }
     }
    
    //必需
    //使用Microsoft.AspNetCore.Http;
    //使用Newtonsoft.Json;
    公共静态类SessionExtensions
    {
    公共静态无效集(此ISession会话,字符串键,T值)
    {
    SetString(key,JsonConvert.SerializeObject(value));
    }
    公共静态T Get(此ISession会话,字符串键)
    {
    var值=session.GetString(键);
    返回值==null?默认值:JsonConvert.DeserializeObject(值);
    }
    }
    

    //必需
    //使用Microsoft.AspNetCore.Mvc.ViewFeatures;
    //使用Newtonsoft.Json;
    公共静态类TempDataExtensions
    {
    公共静态无效集(此ITempDataDictionary tempData、字符串键、T值),其中T:class
    {
    tempData[key]=JsonConvert.SerializeObject(值);
    }
    公共静态T Get(此ITempDataDictionary tempData,字符串键),其中T:class
    {
    对象o;
    tempData.TryGetValue(键,输出o);
    返回o==null?null:JsonConvert.DeserializeObject((字符串)o);
    }
    }
    
  • 使用会话或临时数据存储复杂对象:

    简历表张贴方式:

         public IActionResult OnPost()
         {
             //set value for Info.
             ...
    
             //store value in session
             HttpContext.Session.Set<ResumeInfo>("Resume", Info);
             // use TempData store the object
             TempData.Set("Resume", Info);
    
             return RedirectToPage("GenerateResume", Info);
    
         }
    
    public IActionResult OnPost()
    {
    //设置信息的值。
    ...
    //在会话中存储值
    HttpContext.Session.Set(“Resume”,Info);
    //使用TempData存储对象
    TempData.Set(“恢复”,信息);
    返回重定向Topage(“GenerateResume”,信息);
    }
    
    然后,GenerateResume页面Get方法中的代码:

     public class GenerateResumeModel : PageModel
     {
         [BindProperty(SupportsGet = true)]
         public ResumeInfo Info { get; set; }
         public void OnGet()
         {
             var resume =  HttpContext.Session.Get<ResumeInfo>("Resume");
    
             var data = TempData.Get<ResumeInfo>("Resume");
             //Console.WriteLine(Info.GetExperiences()[0].Name);
         }
     }
    
    公共类GenerateResumeModel:PageModel
    {
    [BindProperty(SupportsGet=true)]
    公共简历信息{get;set;}
    公共互联网
    {
    var resume=HttpContext.Session.Get(“resume”);
    var data=TempData.Get(“恢复”);
    //Console.WriteLine(Info.GetExperiences()[0].Name);
    }
    }
    
  • 测试结果如下:


    发送信息前是否检查过您的信息是否有子女?您有非常奇怪的代码来添加子项。它工作起来很有魅力……感谢您的详细解释
         public IActionResult OnPost()
         {
             //set value for Info.
             ...
    
             //store value in session
             HttpContext.Session.Set<ResumeInfo>("Resume", Info);
             // use TempData store the object
             TempData.Set("Resume", Info);
    
             return RedirectToPage("GenerateResume", Info);
    
         }
    
     public class GenerateResumeModel : PageModel
     {
         [BindProperty(SupportsGet = true)]
         public ResumeInfo Info { get; set; }
         public void OnGet()
         {
             var resume =  HttpContext.Session.Get<ResumeInfo>("Resume");
    
             var data = TempData.Get<ResumeInfo>("Resume");
             //Console.WriteLine(Info.GetExperiences()[0].Name);
         }
     }