本文介绍了T4MVC OptionalParameter值从当前上下文暗示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了我认为是一些奇怪的行为与T4MVC。具体来说,我试图建立一个ActionLink的(使用的HtmlHelper),一个动作,其中可选的参数值为null。这工作得很好大部分的时间。然而,如果当前的路由是其中的ActionLink正在建立相同的和OptionalParameter具有非空值,将所得的ActionLink将从当前路由上下文指定可选的参数的值。

这是一个罗嗦的解释,我觉得code将有助于澄清。

控制器

 公共虚拟的ActionResult今天(INT?LINENUMBER = NULL)
{
    回报指数(DateTime.Today,DateTime.Today,行号);
}

路线

  context.MapRoute(
    TodaysProductionSchedules
    生产/ {}控制器/今天/ {} LINENUMBER
    新
        {
            面积= AREANAME,
            控制器= MVC.Production.ProductionSchedules.Name,
            行动= MVC.Production.ProductionSchedules.ActionNames.Today,
            LINENUMBER = UrlParameter.Optional
        });

剃刀

  @ Html.ActionLink(显示今日,MVC.Production.ProductionSchedules.Today(NULL))


正如我前面提到的,如果我没有目前它被映射到这条线路视图时,链接将被正确生成。然而,如果当前视图是否映射此路线和I要么省略值或供给空(如见于剃刀片断),则行号参数会从当前路线值其值。

我想这可能是T4MVC个bug,因此我会后对T4MVC codePLEX网站的链接到这个话题为好。在此先感谢!


解决方案

更新7/30/2012 这是固定的T4MVC 2.10.1

这实际上是从模型unbinder变化的最近回归。在周围线639 t4mvc.tt,你可以尝试改变AddRouteValues​​为以下内容:

 公共静态无效AddRouteValues​​(RouteValueDictionary routeValueDictionary,串routeName,对象routeValue){
        IModelUnbinder unbinder;
        如果(routeValue == NULL)
        {
            unbinder = DefaultModelUnbinder;
        }
        其他
        {
            unbinder = ModelUnbinders.FindUnbinderFor(routeValue.GetType())? DefaultModelUnbinder;
        }
        unbinder.UnbindModel(routeValueDictionary,routeName,routeValue);
    }


原来的答复
我一般认为在MVC中,在当从该新路线省略值许多情况下,它从当前路线获取其值,假定高电平值相同(因此看到两种不同的情况)。

所以,现在的问题是,是否可以T4MVC /应该做点什么来避免这种行为。我没有检查的具体逻辑,但也许如果它总是在设定的路线这个值,这将禁用此不必要的行为。

不过,我认为第一步是要充分了解MVC行为,在这里打球是应对T4MVC案前。

随意采取进一步的调查,并派出一个公关与修复! :)

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!

解决方案

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

这篇关于T4MVC OptionalParameter值从当前上下文暗示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:59