0

I am trying to learn binding a complex object in My MVC application. Below is my Class

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

        };
    }

}

I am using the following controller

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

On posting form in EditBindCheck method all the Values are properly binded to user object. But On Passing to PrintStrongUser, user2.firstName and user2.lastName are same as in user object , but user2.myCountry and user2.CountryList are NULL

I dont understand why the object has changed its values on passing to another ActionMethod??

  • You cannot pass an object containing properties which are complex objects or collections using `RedirectToAction()` - you save the data, pass its ID and get the object again in the GET method (and look at the url you are generating to understand why) –  Aug 17 '18 at 11:55
  • You can set any object, model, values into the TempData/ViewData or ViewBag and get/set it across actions (http://www.rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp-net-mvc-3-applications/). Its not the best way but it can solve your issue. Also have a look at this, i find it a GREAT source of information: http://www.tutorialsteacher.com/mvc/asp.net-mvc-tutorials – Dimitri Aug 17 '18 at 12:14
  • try return RedirectToAction("PrintStrongUser", new { user }); i think this would work. – Anmol Rathod Aug 17 '18 at 12:27
  • 1
    @Dimitri, Only `TempData` can be used to transer data between actions (not `ViewBag` or `ViewData`) - and you right in that its not the best (if the user refreshes the browser, the data will be lost (unless using `.Keep()`) –  Aug 17 '18 at 13:34
  • @Stephen Muecke, you are absolutely right! – Dimitri Aug 17 '18 at 13:53

0 Answers0