T4MVC OptionalParameter values implied from curren

2019-06-20 18:25发布

问题:

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!

回答1:

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! :)