MVC model binding: using a name other than QueryString for a method parameter
Given the url:
http://www.stackoverflow.com/question ask = 123 & answers = 5
and the 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;
}
}
Note that QueryString and RequestObject parameters are different. Can I achieve this with the default binding behavior? Do I need to create a custom binder?
Thank!
+2
user129393
source
to share
3 answers
It looks like a custom linker is what you want. Scott Hanselman has a good example of implementing custom binders here
+1
Mac
source
to share
You can use explicit object initialization:
public ActionResult Question(string ask, string answers)
{
return View("Question", new RequestObject
{
AskId = ask,
NumberOfAnswers = answers
});
}
0
Alexander Prokofyev
source
to share
Override DefaultModelBinder. In particular, his method BindProperty
.
0
Anthony Serdyukov
source
to share