Strongly-typed T4MVC Action/ActionLink

后端 未结 2 1144
天命终不由人
天命终不由人 2021-02-04 04:33

I\'ve been using T4MVC (FYI: v2.6.62) for quite some time, and I\'ve been slowly moving over our code to this way of working (less reliance on magic strings).

But I\'ve

2条回答
  •  忘了有多久
    2021-02-04 05:19

    If I've understood the problem correctly then the following syntax should allow you to work around the problem.

    <%= Html.ActionLink("test", MVC.MyController.MyAction().AddRouteValues(new MyClass() { Number = 5, SomeText = "Hello" })) %>
    

    I think the answer to make the syntax nicer would be to wrap each non value type parameter in a RouteValueDictionary in each generated action result method

    Edit: (Response to comment as not enough chars)

    Ah ok I managed to recreate the simple example above using this method to give: /MyController/MyAction/5/Hello as the url. I'm not quite sure how nested complex types would pan out in practice. You could use some recursion to dive down the into the top-level object and reflect over the values to add them but then you open up a new set of issues, such as how to cope with a child property name that is identical to the parent property name. This seems like it could be a complex problem to solve, in a manner that would work for everyone. Perhaps some kind of adapter pattern would be most useful to transform a complex object into route values. In the simplest case this might be to declare an extension method ToRouteDictionary that acts on your complex type and transforms it using your knowledge of how it should work. Just thinking out loud as I'm obviously not aware of your use cases

提交回复
热议问题