开发者

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

I am wanting to pass something like the following to my view from my controller via 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 controller would like like this. (Note it is a GET):

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

The problem is that the instance of MyObject is coming back as null, where val1 and val2 have data. Has anyone run across this?


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

Note that the sub-object propety names need to be prefixed with the parent's property name (obj in this case)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜