pixelmike
pixelmike

Reputation: 1983

How to get Html.RenderAction to use my parameters?

I have this in my RouteConfig.cs:

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

In PromoController.cs:

[ActionName("Index")]
public ActionResult Index( string slug = "" )
{
    // code that uses slug
}

Then in a cshtml page I'm trying to do:

@{Html.RenderAction("Index", "Promo", "drink");}

But the "drink" parameter is not getting passed to my controller.

Upvotes: 1

Views: 4327

Answers (2)

David
David

Reputation: 34573

You have to specify that "drink" should be passed to the slug parameter:

@{Html.RenderAction("Index", "Promo", new { slug = "drink" });}

You can leave out "Promo" as long as you are staying within the same controller, but the action name is always required.

Upvotes: 2

Alex Art.
Alex Art.

Reputation: 8781

This should work:

@{Html.RenderAction("Index", "Promo", new { slug = "drink"});}

Upvotes: 2

Related Questions