user34537
user34537

Reputation:

Get route {id} value in view?

I known if I have something like /controller/action/{id} I can access id as a function parameter. But how would I access it via the view without using the viewbag?

Upvotes: 1

Views: 2014

Answers (3)

user34537
user34537

Reputation:

It will be put directly into the ViewBag.id or you can access it via ViewData["id"]. hey seem like the same object

Upvotes: 0

Glenn Ferrie
Glenn Ferrie

Reputation: 10390

You can pass that function parameter to the View as part of a model. The model can be staticly or dynamically typed. The example code below demonstrates how to pass the value as a property on a dynamic model.

public ActionResult Edit(string id)
{
    dynamic model = new System.Dynamic.ExpandoObject();
    model.Id = id;
    return View(model);

}

You would access this value in the view as follows:

@Model.Id

Upvotes: 2

Victor
Victor

Reputation: 666

You can parse the URL to get the ID when in the view:

var id = Request.Url.LocalPath.SubString(LastIndexOf("/",Request.Url.LocalPath)+1);

Upvotes: 0

Related Questions