Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4MVC OptionalParameter values implied from current context

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!

like image 747
Vinney Kelly Avatar asked Jul 19 '12 17:07

Vinney Kelly


People also ask

What is T4 parameter in Visual Studio Code?

T4 Parameter Directive. In a Visual Studio text template, the parameter directive declares properties in your template code that are initialized from values passed in from the external context. You can set these values if you write code that invokes text transformation.

What is the difference between outputparameters and target and optionalparameters?

It will have two keys Target and OptionalParameters Target property will be passed as a DynamicEntity and represents the image of the data passed. OutputParameters is populated by the platform and only contains valid data during the After Operation stage. This will contain the properties of the response message.

How do you use the parameter directive in a template?

Using the Parameter Directive. The parameter directive declares properties in your template code that are initialized from values passed in from the external context.


1 Answers

Update 7/30/2012: This is fixed in T4MVC 2.10.1!

This was actually a recent regression from the model unbinder change. In t4mvc.tt around line 639, can you try changing AddRouteValues to the following:

    public static void AddRouteValues(RouteValueDictionary routeValueDictionary, string routeName, object routeValue) {
        IModelUnbinder unbinder;
        if (routeValue == null)
        {
            unbinder = DefaultModelUnbinder;
        }
        else
        {
            unbinder = ModelUnbinders.FindUnbinderFor(routeValue.GetType()) ?? DefaultModelUnbinder;
        }
        unbinder.UnbindModel(routeValueDictionary, routeName, routeValue);
    }

Original answer: I think generally in MVC, in many scenarios when a value is omitted from the new route, it gets its value from the current route, assuming that the high level values are the same (hence the two different cases you see).

So now the question is whether T4MVC can/should do something to avoid this behavior. I haven't checked the exact logic, but maybe if it always set this value in the route, that would disable this unwanted behavior.

But I think the first step is to fully understand the MVC behavior that's at play here before tackling the T4MVC case.

Feel free to take the investigation further and send a PR with the fix! :)

like image 145
David Ebbo Avatar answered Nov 15 '22 08:11

David Ebbo