1

I had the following Action:

public ActionResult GetCityObjects(string cityAlias)

By some reasons I added a custom ModelBinder:

public ActionResult GetCityObjects(City city)

Now I want to make T4MVC add "cityAlias" parameter with value city.Alias when I pass city parameter to appropriate T4MVC method. Is there any way to achieve it?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266

3 Answers3

3

It's now possible with T4MVC Model Unbinder feature (http://t4mvc.codeplex.com/documentation 3.1), you could implement custom unbinder for City type like that:

public class CityUnbinder : IModelUnbinder<City>
{
    public void UnbindModel(RouteValueDictionary routeValueDictionary, string routeName, City city)
    {
        if (user != null)
            routeValueDictionary.Add("cityAlias", city.Alias);
    }
}

and then register it in T4MVC (from Application_Start):

ModelUnbinderHelpers.ModelUnbinders.Add(new CityUnbinder());

After that you can normally use MVC.Home.GetCityObjects(city) for generating urls.

Shaddix
  • 5,901
  • 8
  • 45
  • 86
2

I don't think so.

You need to use the parameterless version and add route values manually:

GetCityObjects().AddRouteValue("cityAlias", city.cityAlias)

If you look at the source code you will see that the generated method just adds city instance using parameter name 'city'.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
0

I have found a workaround. I have hardcoded the following to T4MVC:

<#foreach (var method in controller.ActionMethods) { #>
        public override <#=method.ReturnTypeFullName #> <#=method.Name #>(<#method.WriteFormalParameters(true); #>) {
            var callInfo = new T4MVC_<#=method.ReturnType #>(Area, Name, ActionNames.<#=method.ActionName #>);
<#if (method.Parameters.Count > 0) { #>
<#foreach (var p in method.Parameters) { #>
<#  if (p.Name != "city") { #>
        callInfo.RouteValueDictionary.Add(<#=p.RouteNameExpression #>, <#=p.Name #>);
<# } #>
<# else #>
<# { #>
       callInfo.RouteValueDictionary.Add("cityAlias", city.Alias);
<# } #>
<#} #>
<#}#>
            return callInfo;
        }

I can't say I like it, but at least it works in my case.

David, what do you think about introducing a more general implementation of this into T4MVC?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • That sounds good in principle! Just need to find contributors who have time to work on this, as unfortunately I only have limited resources to work on T4MVC. Though I try to fix bugs with obvious fixes quickly. – David Ebbo Feb 17 '12 at 00:01
  • @DavidEbbo, I hope I can find some time to implement it, but first I would like to discuss what to implement. It would be great to generalize similar cases and find out a best way to cover them – SiberianGuy Feb 17 '12 at 05:16