I've noticed what I believe to be some odd behavior with T4MVC. Specifically, I'm attempting to build an ActionLink (using the HtmlHelper) for an action where the optional parameter value is null. This works fine most of the time. However, if the current route is of the same for which the ActionLink is being built AND the OptionalParameter has a non-null value, the resulting ActionLink will specify the value of the optional parameter from the current route context.
That's a wordy explanation, I think code will help clarify.
Controller
public virtual ActionResult Today(int? lineNumber = null)
{
return Index(DateTime.Today, DateTime.Today, lineNumber);
}
Route
context.MapRoute(
"TodaysProductionSchedules",
"Production/{Controller}/Today/{lineNumber}",
new
{
area = AreaName,
controller = MVC.Production.ProductionSchedules.Name,
action = MVC.Production.ProductionSchedules.ActionNames.Today,
lineNumber = UrlParameter.Optional
});
Razor
@Html.ActionLink("Show Today", MVC.Production.ProductionSchedules.Today(null))
As I mentioned earlier, if I am not currently on a view which is mapped to this route, the link will be generated correctly. However, if the current view does map the this route AND I either omit the value or supply null (as seen in the razor snippet), the lineNumber parameter will take its value from the current route value.
I think this might be a bug in T4MVC so I'll post a link to this topic on the T4MVC codeplex site as well. Thanks in advance!