views:

158

answers:

3

Given a URL:

http://www.stackoverflow.com/question?ask=123&answers=5

and its corresponding ActionMethod and Model:

public ActionResult Question(RequestObject request)
{
   return View("Question", request);
}

public class RequestObject
{
   public string AskId
   {
      get;
      set;
   }

   public string NumberOfAnswers
   {
      get;
      set;
   }
}

Notice that the QueryString and the parameters of the RequestObject are different. Can I achieve that with the default binding behavior? Do I need to create a custom binder?

Thanks!

A: 

You could use explicit object initialization:

public ActionResult Question(string ask, string answers)
{
    return View("Question", new RequestObject
    {
        AskId = ask,
        NumberOfAnswers = answers
    });
}
Alexander Prokofyev
That's what I have at this point, but I want to move to MVC Model Bindings as I have not just two arguments but several.
Michelle T.
A: 

Override DefaultModelBinder. Particularily, its BindProperty method.

Anton
+1  A: 

It sounds like a custom model binder is what you want. Scott Hanselman has a good example of implementing custom binders here

Mac