簡體   English   中英

ASP.NET C# class 中的字典在傳遞給另一個 ZC1C425268E68385D1AB5074F4ZA 時接收為空

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

我確定這是一個相當新手的問題,但我沒有看到其他有用的帖子,所以請提供一點幫助。

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.

請問,我錯過了什么?

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

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

代替:

return RedirectToAction("Reparto", DatosReparto);  

稱呼:

return Reparto(DatosReparto);  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM