Reputation: 25302
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?
Upvotes: 1
Views: 375
Reputation: 6109
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.
Upvotes: 3
Reputation: 25302
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?
Upvotes: 0
Reputation: 46008
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'.
Upvotes: 2