微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决传递给另一个函数时,类中的 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);  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。