传递给另一个函数时,类中的 ASP.NET C# 字典被接收为空

问题描述

我确定这是一个新手问题,但我没有看到其他有用的帖子,所以请提供一点帮助。

我在一个类中有一个字典,当我创建一个新对象并将它传递给另一个 ASP.NET 函数时,除了没有元素的字典外,该类被正确通知

拜托,我还想做什么??

这是课堂

    public class Reparto
    {
        [BindProperty]
        public string ID { get; set; }
        public Dictionary<string,string> LecturaSalida { get; set; }
        [BindProperty]
        [display(Name = "Lectura")]
        public string Lectura { get; set; }
        [BindProperty]
        public string JsonDictionary { get; set; }

        public Reparto()
        {
            //MVC asks for this
        }

        public Reparto(string ID,Dictionary<string,string> LecturaSalida)
        {
            this.ID = ID;
            this.LecturaSalida = new Dictionary<string,string>(LecturaSalida);
            this.JsonDictionary = JsonSerializer.Serialize(LecturaSalida); // test to confirm dict is received
        }
    }

这是asp控制器代码(简化)

       [HttpPost]
        public IActionResult Index(string ID)
        {
            try
            {
                Reparto DatosReparto = new Reparto(ID,Context.ObtenerReparto(ID));  // This populates DatosReparto object correctly
                return RedirectToAction("Reparto",DatosReparto);                    // And goes to the next point 
            }
            catch 
            {
                ModelState.AddModelError("ID","Invalid!");
            }
            return View();
        }


        public IActionResult Reparto(Reparto DatosReparto) 
        {
            int test = DatosReparto.LecturaSalida.Count; // zero elements!!! while the other properties are correctly informed,as JsonDict which is correctly informed 4ex.

            return View();
        }

解决方法

代替:

return RedirectToAction("Reparto",DatosReparto);  

调用:

return Reparto(DatosReparto);