1

I am trying to pass 3 parameters, one int, and 2 datetime to a controller with no luck. I have created custom Routes, but no matter what I do it never connects. In My View I have

@Html.ActionLink("Check Availability", "Check", new { id = item.RoomID, AD = ArrivalDate.Date.ToString("dd-MM-yyyy"), DD = DepartureDate.Date.ToString("dd-MM-yyyy") }, null)

In my Controller I have

   [RoutePrefix("RoomInfoes")]
   [Route("Check/{ID},{AD},{DD}")]
    [HttpPost]
    public ActionResult Check(int? id, string AD, string DD) {

I have tried numerous arrangements of routes and views, but never can connect.

The code above returns a 404 with

Requested URL: /RoomInfoes/Check/1,06-11-2014,06-11-2014

Thanks

3
  • Does it work if you remove the dates and pass only the int? By remove, I mean on the controller as well. Commented Nov 6, 2014 at 0:19
  • Yep, the int is passed. But no other parameters. Commented Nov 6, 2014 at 0:58
  • What does your link look like when hovered. BTW i couldnt get this to work at all with [HttpPost], only as a get request even when using ajax.actionlink and setting it to be post. It works fine for me as a get request, which suggests that the params you have are right, I don't have the routeprefix in either. Does it work for you without that? Commented Nov 6, 2014 at 2:11

2 Answers 2

1

Before you use attribute routing make sure you have it enabled:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes(); // add this line in your route config

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

then decorate your action like this:

[Route("RoomInfoes/Check/{ID},{AD},{DD}")]
public ActionResult Test(int? id, string AD, string DD)

The RoutePrefix has been removed because it can be used only on class declaration (the code will even doesn't compile). I removed the HttpPost attribute because I assumed that you want make a GET instead of POST.

then to generate a link pointing to this action you can simply write:

@Html.ActionLink("test", "Test", "Home", new { id = 5, AD="58", DD = "58" }, null)

the result will be:

<a href="/RoomInfoes/Check/5%2c58%2c58">test</a> (the commas in your url will be url encoded)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your comprehensive reply! I really appreciate the time you took to make it
1

Do also have the Check Action method for serving HttpGet requests? If not, you will get 404 error.

Have you also done the routes.MapRoute in the RouteConfig.cs? This is required to render the correct URL with @Html.ActionLink helper method.

Try adding below code in RouteConfig.cs, if not already exists.

routes.MapRoute(
            name: "RoomInfoes",
            url: "Check/{ID},{AD},{DD}",
            defaults: new
            {
                controller = "RoomInfoes",
                action = "Check",
                ID = UrlParameter.Optional,
                AD = UrlParameter.Optional,
                DD = UrlParameter.Optional
            }
            );

You don't Need Route and the RoutePrefix attribute on the Action method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.