I am trying to use jQuery $ .getJSON to access model data from a Controller and create a JSON object in javascript to use as a user who adds and removes items from a list. All JS is executed in an external file. An attack on the server for model data is performed only once after loading the first page, so I can get a complete list of options for the user to select. After that, the user can change the list as much as he wants (and the corresponding JSON object will be updated accordingly), and then save it (written back to the database).
But here is my problem: when using $ .getJSON, the method that I need to call in the controller must have an identifier so that it can look for which specific resource I'm talking about and return a list of parameters related to this specific resource. But from an external JS file, I do not know how to get this identifier for accessing the controller. It is embedded in the link that opens the page in the first place, and sits in the URL, but as soon as the page loads, I don’t know how to get this identifier in order to pass it to the controller. I pasted some of my code below because I know this is confusing:
A detailed method in the controller that initially loads the page (accepts the resourceID to know what needs to be loaded):
public ActionResult Details(string resId)
{
base.Details(resId, resourceType);
Evaluation eval = (Evaluation)this.Model;
ViewData.Model = eval;
return View("Index");
}
In my external JS file, I do this:
$.getJSON("/Evaluation/PopulateJournalOptions/" + id, populateJSON);
PopulateJournalOptions, JSON:
public JsonResult PopulateJournalOptions(string resId)
{
JournalOptionsResult jsonResult = new JournalOptionsResult();
base.Details(resId, resourceType);
Evaluation eval = (Evaluation)this.Model;
jsonResult.Activities = eval.Journal.Activities;
jsonResult.Issues = eval.Journal.Issues;
jsonResult.ScheduledActions = eval.Journal.ScheduledActions;
return Json(jsonResult);
}
, , , PopulationJournalOptions. PopulationJournalOptions (Evaluation)this.Model, this.Model , , Controller JS, . ?