คุณสมบัติ Model Binding Object ได้รับ NULL เมื่อส่งผ่านขณะเปลี่ยนเส้นทางไปยังวิธีดำเนินการอื่น [ซ้ำกัน]

ฉันกำลังพยายามเรียนรู้การเชื่อมโยงวัตถุที่ซับซ้อนในแอปพลิเคชัน My MVC ด้านล่างคือชั้นเรียนของฉัน

public class StrongUser
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Country myCountry { get; set; }
    public List<Country> CountryList { get; set; }
    public StrongUser()
    {
        firstName = "Naveen";
        lastName = "Verma";
        myCountry = Country.ListOfCountries()[0];
        CountryList = Country.ListOfCountries();
    }

    public static StringBuilder Print(StrongUser user2)
    {
        StringBuilder sr = new StringBuilder();
        sr.AppendLine("First Name=" + user2.firstName);
        sr.AppendLine("<br>");
        sr.AppendLine("Last Name=" + user2.lastName);
        sr.AppendLine("<br>");
        sr.AppendLine("My Country Name=" + user2.myCountry.Name);
        sr.AppendLine("<br>");
        sr.AppendLine("My Country Continent=" + user2.myCountry.Continent);
        sr.AppendLine("<br>");
        sr.AppendLine("My Country List:");
        sr.AppendLine("<br>");
        foreach (Country c in user2.CountryList)
        {
            sr.AppendLine("<br>");
            sr.AppendLine("Name=" + c.Name);
            sr.AppendLine("<br>");
            sr.AppendLine("Continent=" + c.Continent);
        }
        return sr;
    }
}

public class Country
{ public int Id { get; set; }
  public string Name { get; set; }
  public string Continent { get; set; }

    public static List<Country> ListOfCountries()
    {
        return new List<Country>()
        {
            new Country(){Id=1, Name ="India",Continent="Asia"},
            new Country(){Id=2,Name="USA",Continent="North America"},
            new Country(){Id=3,Name="Japan",Continent="Asia"},
            new Country(){Id=4,Name="UK",Continent="Asia-Pacific"},
            new Country(){Id=5,Name="Australia",Continent="Australia"},
            new Country(){Id=6,Name="China",Continent="Asia"}

        };
    }

}

ฉันใช้คอนโทรลเลอร์ต่อไปนี้

    [HttpGet]
     public ActionResult EditBindCheck()
    {
        return View(new StrongUser());
    }

    [HttpPost]
    public ActionResult EditBindCheck(StrongUser user)
    {
//here all the values are properly binded to **user** object

        return RedirectToAction("PrintStrongUser", user);
    }

    public StringBuilder PrintStrongUser(StrongUser user2)
    {

        return StrongUser.Print(user2);
    }

ในการโพสต์แบบฟอร์มในวิธี EditBindCheck ค่าทั้งหมดจะถูกผูกไว้กับวัตถุ ผู้ใช้ อย่างถูกต้อง แต่ในการส่งต่อไปยัง PrintStrongUser, user2.firstName และ user2.lastName จะเหมือนกับใน user object แต่ user2.myCountry และ user2.CountryList จะเป็น NULL

ฉันไม่เข้าใจว่าเหตุใดวัตถุจึงเปลี่ยนค่าเมื่อส่งผ่านไปยัง ActionMethod อื่น


person naveen verma    schedule 17.08.2018    source แหล่งที่มา
comment
คุณไม่สามารถส่งผ่านวัตถุที่มีคุณสมบัติซึ่งเป็นวัตถุที่ซับซ้อนหรือคอลเลกชันโดยใช้ RedirectToAction() - คุณบันทึกข้อมูล ส่ง ID ของมัน และรับวัตถุอีกครั้งในวิธีการ GET (และดูที่ URL ที่คุณกำลังสร้างเพื่อทำความเข้าใจว่าทำไม)   -  person    schedule 17.08.2018
comment
คุณสามารถตั้งค่าออบเจ็กต์ โมเดล ค่าใดๆ ลงใน TempData/ViewData หรือ ViewBag และรับ/ตั้งค่าข้ามการดำเนินการ (rachelappel.com/) มันไม่ใช่วิธีที่ดีที่สุดแต่สามารถแก้ปัญหาของคุณได้ ลองดูที่นี้ด้วย ฉันคิดว่ามันเป็นแหล่งข้อมูลที่ยอดเยี่ยม: tutorialsteacher com/mvc/asp.net-mvc-tutorials   -  person Dimitri    schedule 17.08.2018
comment
ลอง return RedirectToAction(PrintStrongUser, new { user }); ฉันคิดว่ามันจะได้ผล   -  person Anmol Rathod    schedule 17.08.2018
comment
@Dimitri เพียง TempData เท่านั้นที่สามารถใช้เพื่อถ่ายโอนข้อมูลระหว่างการกระทำ (ไม่ใช่ ViewBag หรือ ViewData) - และคุณถูกต้องว่ามันไม่ดีที่สุด (หากผู้ใช้รีเฟรชเบราว์เซอร์ ข้อมูลจะสูญหาย (เว้นแต่ใช้ .Keep())   -  person    schedule 17.08.2018
comment
@Stephen Muecke คุณพูดถูกจริงๆ!   -  person Dimitri    schedule 17.08.2018