How to pass complex objects to ASP.NET MVC using Get parameters?

I want to transfer something like the following from my controller: GET not POST:

public class MyDTO
{
   public string val1 { get; set; }
   public string val2 { get; set; }
   public MyObject obj { get; set; }
}

public class MyObject
{
   public int SomeInt { get; set; }
   public string ACoolValue { get; set; }
   public string YetAnotherCoolValue { get; set; }
}

And then the dispatcher will like it like that. (Note that this is a GET):

public ActionResult MyView(MyDTO dto)
{
   return View(dto)
}

The problem is that the MyObject instance is returned as null, where val1 and val2 have data. Does anyone come across this?

+3
source share
1 answer

I just created a brand new ASP.NET MVC 2 website in Visual Studio 2010, added two class definitions and changed the HomeController About action to a parameter dto. When I go to the URL /Home/About?val1=aaa&val2=bbb&obj.SomeInt=111&obj.ACoolValue=ccc&obj.YetAnotherCoolValue=ddd, all properties are populated.

, - (obj )

+3

Source: https://habr.com/ru/post/1745292/


All Articles