Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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!

share|improve this question
    
I guess I'll continue reading up more on routing.. I'm sure that's where this issue lies. – notsoobvious Apr 2 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} . – gerdi Apr 3 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}" – gerdi Apr 3 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: "localhost:51245/Method/Branding?Url=url-name/2";, For example. – notsoobvious Apr 3 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 3 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.