简体   繁体   English

ASP.NET C# class 中的字典在传递给另一个 ZC1C425268E68385D1AB5074F4ZA 时接收为空

[英]ASP.NET C# Dictionary inside a class is received empty when passed to another function

I'm sure it's quite a newbie question, but I see no other useful post, so pls, a little help will be appreciated.我确定这是一个相当新手的问题,但我没有看到其他有用的帖子,所以请提供一点帮助。

I have a Dictionary inside a Class, and when I create a new object and pass it to another ASP.NET function, the class is correctly informed except for the dictionary, which has no elements. I have a Dictionary inside a Class, and when I create a new object and pass it to another ASP.NET function, the class is correctly informed except for the dictionary, which has no elements.

Please, what am I missing to do??请问,我错过了什么?

This is the class这是 class

    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
        }
    }

and this is the asp controller code (simplified)这是 asp controller 代码(简化)

       [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();
        }

Instead of:代替:

return RedirectToAction("Reparto", DatosReparto);  

call:称呼:

return Reparto(DatosReparto);  

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

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