haansi
haansi

Reputation: 5736

Passing routed value to View from Action

Below is my action code, which receive a parameter (int id).

public virtual ActionResult Index(int id)
{
    // Logic
    return View("List", id);
}

My URL is something like: Home/List/1

I know I can return View("List") but don't know how to ad id with it, please help me.

Upvotes: 0

Views: 144

Answers (4)

banny
banny

Reputation: 919

If you are using routes like this

    //products
routes.MapLocalizedRoute("Product","s/{storeid}/{storename}/p/{productId}/{SeName}",
    new { controller = "Catalog", action = "Product", SeName = UrlParameter.Optional },
    new { productId = @"\d+", storeId = @"\d+" }, 
    new[] { "Nop.Web.Controllers" });

then we can use like this.

return RedirectToRoute("Product",new { storeid=123, storename="name",productId=23,SeName="something"});

Upvotes: 0

banny
banny

Reputation: 919

return RedirectToAction("List", new { id =  Id }) 

Upvotes: 0

Anuraj
Anuraj

Reputation: 19598

You can use something like Controller.RedirectToAction Method

return RedirectToAction("List", new { Id = id});

Upvotes: 6

Mats
Mats

Reputation: 14817

In your controller logic you most likely would want take the id parameter to fetch some data/do some processing and then return a model to your view.

Please check the this link for some fine tutorials on MVC.

If you just want to pass the ID from the route to the view, you could either use the ViewBag or have your View accept an int model, which you then could pass like

return View("List", id);

Upvotes: 0

Related Questions