Prd
Prd

Reputation: 247

passing variable to view ASP MVC

I have 2 views, one to return all the locations belonging to a project, the other returns a json file containing the locations that used to show them on a google map. Listing the locations works like this as the id is sent with the actionlink, but how do I send the project ID to the Map view?

public ActionResult GoogleMaps(int id)
  {
      return View(Project.Find(id).DeviceLocations);
  }


  //[AutoRefresh(DurationInSeconds = 30)]

  public ActionResult Map(int id)
  {
      var map = Project.Find(id).DeviceLocations;
      Locations l = new Locations();
      l.locations = map;
      return Json(l, JsonRequestBehavior.AllowGet);
  }

Upvotes: 0

Views: 1920

Answers (2)

Kanishka
Kanishka

Reputation: 143

Include a get, set property "project ID" in your Locations class.

Upvotes: 0

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171411

Some people construct a model (class) specifically to handle all of the values being passed from the controller. So, your model would have a property called DeviceID, and would thus be strongly typed.

Alternately, you can use ViewData:

You can put this in your controller:

ViewData["DeviceID"] = id;

Then, in your view, you will need to cast it before you use it, like this:

(int)ViewData["DeviceID"]

Upvotes: 2

Related Questions