views:

26

answers:

1

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?

+2  A: 

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)

Daniel Renshaw
hmmm. It is not doing that in my case. What I am doing is from a previous ActionResult, I am doing a RedirectToAction("MyView", dto). Before I do this I have data in the MyObject instance, but when I redirectToAction, I lose it. I am setting a break point in MyView and seeing null.
Misnomer
What does the URL look like after the redirect, are the values there? are their names prefixed correctly?
Daniel Renshaw
Misnomer
Hmm, I see the same behaviour when using RedirectToAction. My gess is that you'll need a custom ModelBinder but I'm suprised the default model binder isn't working here.
Daniel Renshaw
Well, can you point me in the direction to a good article on custom model binder...thanks.
Misnomer