2

MVCアプリにカスタムモデルバインダーがありますが、T4MVCを使用できるかどうかわかりません。

通常、私は自分の行動をこのように呼びます:

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

T4MVCでは、次のようになります。

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

しかし、T4は私のバインダーについて知らないので、彼はURLでオブジェクトを送信しようとしますが、私が欲しいのは、次のようなURLを生成することです:Contract / {contractId} / Version / {action} / {version}

また、カスタムルートがあることに注意してください。

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

これは私のバインダーです:

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;
        }

私は何をすべきか?ルートにマジックストリングを入れたくない...

ありがとう!

4

2 に答える 2

3

次のようなことを試してください:

return RedirectToAction(MVC.Version.Edit().AddRouteValues(new {contractId = contract.Id.ToString()}));
于 2011-04-18T04:56:48.503 に答える
1

そして今、同じことがModelUnbindersで達成できます。カスタム unbinder を実装できます。

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

次に、T4MVC に登録します (Application_Start から):

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

その後、通常は MVC.Version.Edit(contract) を使用して URL を生成できます。

于 2012-08-20T06:29:23.490 に答える