views:

314

answers:

4

I've been using T4MVC (FYI: v2.6.12) 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.

+2  A: 

Copying my reply from the forum thread (http://forums.asp.net/t/1532611.aspx):

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?

David Ebbo
Hi David. Thanks for the reply. I'm guessing that, given the 5 upvotes in the brief period it's been up means that this is probably desirable functionality. :-)
Dan Atkinson
Ok, I'll put it on the TODO list! :)
David Ebbo
Thank you! I would find it especially useful as I've created a few ActionResults, such as PermanentRedirectResult which utilise the T4MVC style actions, and this sort of functionality would be great, not just for using in views!
Dan Atkinson
+2  A: 

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

PabloBlamirez
I'm afraid this doesn't work. It returns the route values, but they url isn't formed correctly.Also, if it is fixed using this method of wrapping non value type, you'll need to do so recursively, because there you could be using a complex type that contains other complex types...
Dan Atkinson
edited in response to above comment
PabloBlamirez
A: 

What : A loosely typed programming languages is typically one that does not require the data type of a variable to be explicitly stated.

Example : As in JavaScript we can declare var count = 1; which will automatically treat variable count as integer.

Advantage: The advantage of loosely typed is that we don't have to bother about the data type of the variable value and we have the benefit of changing the type of the variable at any instance of time. Usage in C# : ASP.NET 3.5 gives a new keyword “var” which behaves similar to what “var” keyword seems to do in JavaScript. By using the var keyword we can implicitly convert to its originally data type. So, var firstName = "Aditya"; convert the data type of variable “firstName” to string data type implicitly.

eliza sahoo
I'm sorry, but what does this have to do with the question??
Dan Atkinson
A: 

It's not an answer rather asking a question to your experience. I also use complex types to pass them to url string but found that they are not always render as desired. To solve this problem I do use overriden ToString method of passed complex type. It works but I don't like that solution - it looks ugly for me and I already asked a question here but got no answer. Could you explain please how have you resolved such issue?

Cheburek