0

In my view I'm using a

<img src="@Url.Action("Method", new { Model.Url })" ...
<img src="@Url.Action("Method", new { Model.Url })/1" ...

And in that method I have params:

public ActionResult Method(string url, int ids=0)

With

var book = GetBookByUrl(url);
...
switch (ids)
{
case 1:

In my global.asax I have:

        routes.MapRoute(null,
            "{controller}/{action}/{url}/{ids}",
            new { controller = "Controller", action = "Method", url = "", ids = @"\d+" });

My view returns the correct URL's (with /1 and /2 appended), but I can't seem to get my method to retrieve the value of ids so that I can manipulate them in my action method to retrieve different results based on the Url.Action URL provided within my view, what gives?

The ids parameter seems to always be 0 (not being routed data) and the url parameter has the entire url "bla-bla/2", which I do not want.

Please note that although there are plenty of routing resources, I've read over them and can't seem to get this to work - also is this the standard way of routing data from a request to a controller?

Please show me by way of example how to solve, many thanks!

notsoobvious
  • 175
  • 1
  • 9
  • I guess I'll continue reading up more on routing.. I'm sure that's where this issue lies. – notsoobvious Apr 02 '15 at 22:46
  • Why are you defining a route in your `Global.asax`? Also the `@Url.Action` holds 3 parameters in your case. `Action`,`Controller`,`route values`. I think you just need to add the controller name because currently your controller name is `new {Model.Url} ` . –  Apr 03 '15 at 12:16
  • Also the `/1` is a route value so it should be contained in the `@Url.Action` as a parameter in the new{} . I would say `@Url.Action("ACTION","CONTROLLER",new{ url = Model.Url, ids = Model.Ids}"` –  Apr 03 '15 at 12:18
  • @gerdi Global.asax is used for older MVC projects, hence no routeconfig here (it's also not necessary as it's a simple backoffice thing). Also I didn't specify the controller name as I thought it implicit - that the action method lay in the same .cs file to the controller which has the corresponding view that is in use. Indeed I've verified this by checking the actual URLs and they don't change: "http://localhost:51245/Method/Branding?Url=url-name/2", For example. – notsoobvious Apr 03 '15 at 12:25
  • The ViewModel that is statically referenced at the top of the view file has no concept of the ids, so Model.Ids won't work. What I'm trying to do is say "send a unique flag for each element in the view file (like an integer) to the action method so that I can process it differently with my logic". – notsoobvious Apr 03 '15 at 12:31
  • Oh yeah, you are right, didnt know it was referencing the same controller. wouldnt it then be `@Url.Action("Method", new { Model.Url })/?ids=1"` . –  Apr 03 '15 at 12:38
  • I've tried that and put a debug point in the editor and hit f5 in my browser - the "url" string parameter is still taking all that information and not breaking it up into segments like I expect - i.e: Url = "bla-bla/ids=1", which I clearly don't want. The ids uses the default (of 0) as defined in the action method as well to verify the above routing issue! – notsoobvious Apr 03 '15 at 12:52
  • Ok ... so when the url hits the controller it reads the parameters and ... the ids parameter is then set to =0. So why should it be anything else? Why not set the default = 0 in the route config and not in the controller instead of it being 1+? –  Apr 03 '15 at 13:17

0 Answers0