T4MVC 模型绑定

发布于 2025-01-05 06:27:38 字数 297 浏览 3 评论 0原文

我执行了以下操作:

public ActionResult GetCityObjects(string cityAlias)

由于某些原因,我添加了一个自定义 ModelBinder:

public ActionResult GetCityObjects(City city)

现在,当我将 city 参数传递给适当的 T4MVC 方法时,我想让 T4MVC 添加值为 city.Alias 的“cityAlias”参数。有什么办法可以实现吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘦怹 2025-01-12 06:27:38

现在可以使用 T4MVC 模型解绑器功能(http://t4mvc.codeplex.com/documentation 3.1),您可以为 City 类型实现自定义解绑定器,如下所示:

public class CityUnbinder : IModelUnbinder<City>
{
    public void UnbindModel(RouteValueDictionary routeValueDictionary, string routeName, City city)
    {
        if (user != null)
            routeValueDictionary.Add("cityAlias", city.Alias);
    }
}

然后在 T4MVC 中注册它(从 Application_Start ):

ModelUnbinderHelpers.ModelUnbinders.Add(new CityUnbinder());

之后就可以正常使用 MVC.Home.GetCityObjects(city) 来生成 url 了。

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.

不离久伴 2025-01-12 06:27:38

我不这么认为。

您需要使用无参数版本并手动添加路由值:

GetCityObjects().AddRouteValue("cityAlias", city.cityAlias)

如果您查看源代码,您会发现生成的方法只是使用参数名称“city”添加 city 实例。

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'.

A君 2025-01-12 06:27:38

我找到了解决方法。我已将以下内容硬编码到 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;
        }

我不能说我喜欢它,但至少它适用于我的情况。

David,您对将更通用的实现引入到 T4MVC 有何看法?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文