Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strongly-typed T4MVC Action/ActionLink

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 had to stop because, for some reason, T4MVC is unable to translate objects into urls, and only seems to be able to work on primitive types (int/string/etc).

Here is an example:

Route breakdown:

/MyController/MyAction/{Number}/{SomeText}

Class:

namespace MyNamespace
{
  public class MyClass
  {
    public int Number { get; set; }
    public string SomeText { get; set; }
  }
}

Controller:

public class MyController
{
  public virtual ActionResult MyAction(MyClass myClass)
  {
    return View();
  }
}

View:

<%= Html.Action(
  T4MVC.MyController.Actions.MyAction(
    new MyClass()
    {
      Number = 1,
      SomeText = "ABC"
    }
 ) %>

The end result is this:

/MyController/MyAction?myClass=MyNamespace.MyClass

and not

/MyController/MyAction/1/ABC

Does anyone else have this problem? Are T4MVC urls like this available?

Question also asked at the ASP.NET Forum.

like image 997
Dan Atkinson Avatar asked Mar 04 '10 17:03

Dan Atkinson


1 Answers

Update (10/11/2012): the recently added support for Model Unbinders (see section 3.1 in the doc) should hopefully cover a lot of these cases.

Original answer:

Copying my reply from the forum thread:

Hmmm, I don't think this has come up yet. Maybe in most cases that people have Action methods that take an object, the object's values come from posted form data, rather than being passed on the URL? In such scenario, the question doesn't arise.

I think in theory T4MVC could be changed to support this. It would just need to promote all the object's top level properties as route values rather than try to use the object itself (obviously, the current behavior is bogus, and is a result of just calling ToString() blindly).

Have others run into this and think it's worth addressing?

like image 93
David Ebbo Avatar answered Sep 17 '22 15:09

David Ebbo