简体   繁体   English

具有T4MVC的自定义模型活页夹

[英]custom model binder with T4MVC

I have a custom model binder in my MVC app but I don't know hos I can use T4MVC with it. 我在MVC应用程序中有一个自定义模型活页夹,但是我不知道我是否可以将T4MVC与它一起使用。

Usualy I would Call my action this way : 通常,我会这样称呼我的行动:

return RedirectToAction("Edit", "Version", new {contractId = contract.Id.ToString()});

With T4MVC it should be like this : 使用T4MVC应该是这样的:

return RedirectToAction(MVC.Version.Edit(contract));

But since T4 does'nt know about my binder, he try to send the object in the url but what I want is that he generate the url like this : Contract/{contractId}/Version/{action}/{version} 但是由于T4不了解我的资料夹,因此他尝试在url中发送对象,但我想要的是他生成的网址如下:Contract / {contractId} / Version / {action} / {version}

Also note that I have a custom route : 另请注意,我有一条自定义路线:

routes.MapRoute(
                "Version", // Route name
                "Contract/{contractId}/Version/{action}/{version}", // URL with parameters
                new { controller = "Version", action = "Create", version = UrlParameter.Optional } // Parameter defaults
            );

This is my binder : 这是我的活页夹:

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var contractId = GetValue(bindingContext, "contractId");
            var version = GetA<int>(bindingContext,"version");

            var contract = _session.Single<Contract>(contractId);
            if (contract == null) 
            {
                throw new HttpException(404, "Not found");
            }
            var user = _authService.LoggedUser();
            if (contract.CreatedBy == null || !contract.CreatedBy.Id.HasValue || contract.CreatedBy.Id.Value != user.Id)
            {
                throw new HttpException(401, "Unauthorized");
            }

            if (contract.Versions.Count < version)
            {
                throw new HttpException(404, "Not found");
            }
            return contract;
        }

What should I do? 我该怎么办? I don't want to have magic string in my route... 我不想在自己的路线上出现魔术串...

Thanks! 谢谢!

尝试这样的事情:

return RedirectToAction(MVC.Version.Edit().AddRouteValues(new {contractId = contract.Id.ToString()}));

And now the same could be achieved with ModelUnbinders . 现在,使用ModelUnbinders可以实现相同的效果。 You could implement custom unbinder: 您可以实现自定义unbinder:

public class ContractUnbinder : IModelUnbinder<Contract>
{
    public void UnbindModel(RouteValueDictionary routeValueDictionary, string routeName, Contract contract)
    {
        if (user != null)
            routeValueDictionary.Add("cityAlias", contract.Id);
    }
}

and then register it in T4MVC (from Application_Start): 然后在T4MVC中注册(从Application_Start):

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

After that you can normally use MVC.Version.Edit(contract) for generating urls. 之后,您通常可以使用MVC.Version.Edit(contract)生成URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM