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(DurationInSec开发者_运维百科onds = 30)]
public ActionResult Map(int id)
{
var map = Project.Find(id).DeviceLocations;
Locations l = new Locations();
l.locations = map;
return Json(l, JsonRequestBehavior.AllowGet);
}
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"]
Include a get, set property "project ID" in your Locations class.
精彩评论