Optional Parameters From Current Context

Oct 18, 2012 at 1:09 AM
Edited Oct 18, 2012 at 1:15 AM

Hey guys,


this is a continuation from the old topic: http://stackoverflow.com/questions/11566207/t4mvc-optionalparameter-values-implied-from-current-context because the fix submitted didn't seem to fix it for me. Url parameters that I wanted to clear with a null still persists.


I traced the problem to the DefaultModelUnbinder because it seems to be adding all the optional parameters into the RouteValueDictionary as nulls. Let's say I have two optional parameters orderby and page. the problem appears when I am at:




and I want to order by created date. The t4mvc part is simple MVC.Some.Url("createddate", null) using the null to wipe out the page variable. The default unbinder will generate the the routevaluedictionary with orderby="createddate" and page=null; The page=null is the problem I think because I get:





Digging down, it calls Html.RouteLink which doesn't seem to like the null part.

Html.RouteLink("asdf", null, null, null, null, null, new { orderby="createddate" }, null) generates the URL:




Html.RouteLink("asdf", null, null, null, null, null, new { orderby="createddate", page="" }, null) generates the URL:





To fix it, I simple changed the AddRouteValues in ModeUnbinderHelpers to skip out on the routevalues that are null.


// IModelUnbinder unbinder = DefaultModelUnbinder;
if (routeValue != null)
    IModelUnbinder unbinder = ModelUnbinders.FindUnbinderFor(routeValue.GetType()) ?? DefaultModelUnbinder;
    unbinder.UnbindModel(routeValueDictionary, routeName, routeValue);
// unbinder.UnbindModel(routeValueDictionary, routeName, routeValue);




Any feedback would be great.

Oct 19, 2012 at 1:19 AM

To make sure there is no ambiguity, can you specify:

  • The exact signature of your action
  • The call that you are making that generates the incorrect link


Oct 19, 2012 at 1:47 AM

So it actually seems like you're having the opposite issue of what that other bug describes.

  • In that case, they needed the null route value to be included, and it wasn't (then with the fix it was)
  • In your case, it is included and you don't want it to be

As I commented on this thread, the difficulty in T4MVC is there is no easy way of explicitly choosing whether you want to pass null or omit it altogether, because what gets passed is driven by the signature of the method, so there is a loss of expressiveness compared to the regular MVC way where you choose exactly what you want to pass.

We'll need to figure out if there is a good way to solve this.