silhouette hustler
silhouette hustler

Reputation: 1763

Pass an ID independent from model MVC/C#

I'd like to pass an id parameter from a controller to a view if possible completely independent from the model this particular view is using. Something like in the example below:

  public ActionResult Tickets(string id)
  {           

        return View("Ticket",id);
  }

And if this is possible, how can i call this id value from view?

Upvotes: 1

Views: 79

Answers (3)

Ian
Ian

Reputation: 30813

You could use ViewBag, ViewData, or TempData to pass the parameter independent from the View Model. To avoid using dynamic, use ViewData or TempData:

public ActionResult Tickets(string id)
{           
    ViewData["id"] = id;
    TempData["id"] = id;
    return View("Ticket");
}

And in the View, calls just as you defines it:

<i>ID:</i>@ViewData["id"]
<i>ID:</i>@TempData["id"]

Here is reference for differences between ViewBag, ViewData, and TempData:

Upvotes: 2

Dmitry
Dmitry

Reputation: 57

In your case it will work, and model will be this id string in View you can use it @model System.String. Also you can pass by ViewBag. In Controller ViewBag.Id=id; in View @ViewBag.Id

Upvotes: 1

user2960398
user2960398

Reputation: 383

You can do like Using View bag

In Controller

public ActionResult Tickets(string id)
  {           
         ViewBag.id= id;
        return View("Ticket");
  }

In View

<b>ID:</b> @ViewBag.id<br />

Upvotes: 2

Related Questions