URL.Action includes id when constructing URL
Asked Answered
S

3

27

I'm using ASP.Net MVC. Here's my code snippets from a controller named Course:

public ActionResult List(int id)
{
    var viewmodel.ShowUrl = Url.Action("Show", "Course");


    ...
}

public ActionResult Show(int id)
{
  ...
}

viewmodel.ShowUrl picks up whatever the value is of the "id" parameter. So viewmodel.ShowUrl becomes "/Course/Show/151" (value of id is 151); I want to be able to set the id part on the client based on user interaction. I want the value of viewmodel.ShowUrl to be "/Course/Show".

This seems like a bug to me. I'm not telling Url.Action to include an id value. It's doing it on its own. If I want to set the id value then I would do something like this:

var viewmodel.ShowUrl = Url.Action("Show", "Course", new {id = somevalue});

So, how do you prevent MVC from adding the id value? I can hardcode viewmodel.ShowUrl to "/Course/Show" but that seems to be a kludgy solution. Thanks.

Sabo answered 17/4, 2012 at 22:23 Comment(1)
I cant remeber exactly but the Routedata is on ViewData and that's why it's passed. I think you can do Url.Action("Show", "Course", new {}); or empty the RouteData on ViewData object.Abigael
D
12

I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
);

Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.

On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.

public ActionResult Show(int? id)
public ActionResult Show(int id = 0)

Otherwise you'll get an error when you try loading the url without the id parameter.

Deci answered 18/4, 2012 at 0:7 Comment(0)
S
35

Just came across the same problem and so you know, you can also just use an empty string:

@Url.Action("Show", "Course", new { id = "" })
Suspire answered 9/7, 2014 at 6:59 Comment(1)
Thanks @Ben. This is the only one that worked for me.Moue
F
16

I know this is old, but I found this first, but didn't like any of these solutions, so I kept looking and found https://mcmap.net/q/273939/-url-action-including-route-values.

You can use UrlParameter.Optional to solve this problem

Url.Action("Show", "Course", new { id = UrlParameter.Optional })

Fraser answered 13/3, 2015 at 17:1 Comment(0)
D
12

I'm guessing in your routing, you're not specifying that id is an optional parameter. Here's the default route in a sample project.

routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } //Parameter defaults
);

Note the inclusion of id = UrlParameter.Optional. Without that, you'd get the behavior you're describing because it thinks the id is mandatory.

On a side note, if your Show action doesn't always have an id then it should be nullable or provide a default.

public ActionResult Show(int? id)
public ActionResult Show(int id = 0)

Otherwise you'll get an error when you try loading the url without the id parameter.

Deci answered 18/4, 2012 at 0:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.