从外部 Javascript 文件访问 ASP.NET MVC 模型数据
我正在尝试使用 JQuery 的 $.getJSON 从控制器访问模型数据,并在 javascript 中构建一个 JSON 对象,以便在用户从列表中添加和删除项目时使用。所有 JS 都是在外部文件中完成的。模型数据对服务器的命中仅在页面首次加载后完成一次,因此我可以获得可供用户选择的完整选项列表。之后,用户可以根据需要修改列表(并且 JSON 对象也会相应更新),然后将其保存(写回数据库)。
但这是我的问题:当使用 $.getJSON 时,我需要在控制器中调用的方法需要一个 ID,以便它可以查找我正在谈论的特定资源并获取与该特定资源相关的选项列表。但是从外部 JS 文件中,我不知道如何将该 ID 传递给控制器。它内置于首先打开页面的链接中,并且位于 URL 中,但是一旦页面加载,我不知道如何获取此 ID 以便将其传递给控制器。我在下面粘贴了一些代码,因为我知道这很令人困惑:
控制器中最初加载页面的详细信息方法(需要一个资源ID来知道要加载什么):
public ActionResult Details(string resId)
{
//call base method
base.Details(resId, resourceType);
Evaluation eval = (Evaluation)this.Model;
ViewData.Model = eval;
return View("Index");
}
在我的外部JS文件中,我正在这样做:
$.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);
}
同样,我的问题是,一旦页面加载,我就无法访问需要传递给 PopulateJournalOptions 的 ID。我尝试过不向 PopulateJournalOptions 传递任何内容,并在那里创建一个新对象并将其设置为等于 (Evaluation)this.Model
,但 this.Model 似乎为 null,可能是因为它是一个新对象一旦从 JS 调用控制器的实例。所以我有点被困在这里了。有什么想法吗?
I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and build a JSON object in the javascript to use as the user adds and removes items from a list. All JS is done in an external file. The hit to the server for the Model data is done only once after the page first loads so I can get the full list of options for the user to choose from. After that, the user can modify the list as much as he wants (and the JSON object is updated accordingly), and then save it off (gets written back to the DB).
But here's my problem: When using $.getJSON, the method I need to call in the Controller needs an ID so that it can look up what particular resource I'm talking about and get back the list of options that pertains to that particular resource. But from an external JS file, I don't know how to get that ID to pass to the Controller. It's built into the link that opens the page in the first place, and is sitting in the URL, but once the page loads I don't know how to get this ID in order to pass it to the Controller. I've pasted some of my code below, because I know this is confusing:
Details method in Controller that initially loads a page (takes a resourceID to know what to load):
public ActionResult Details(string resId)
{
//call base method
base.Details(resId, resourceType);
Evaluation eval = (Evaluation)this.Model;
ViewData.Model = eval;
return View("Index");
}
In my external JS file, I'm doing this:
$.getJSON("/Evaluation/PopulateJournalOptions/" + id, populateJSON);
And in the Controller, I wrote another method called PopulateJournalOptions that gets called to build the JSON object:
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);
}
Again, my problem is that, once the page loads, I don't have access to the ID I need to pass to PopulateJournalOptions. I've tried just not passing anything to PopulateJournalOptions, and creating a new object there and setting it equal to (Evaluation)this.Model
, but this.Model appears to be null, probably because it's a new instance of the Controller once it's called from the JS. So I'm kind of stuck here. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过将 id 放入页面上的隐藏字段中,然后让 javascript 从该字段中读取 id ?
然后,在您看来,
您的模型具有所需的
Id
作为属性Have you thought about putting the id into a hidden field on the page and then having your javascript read the id from the field?
Then, in your view,
With your model having the required
Id
as a property将其放入控制器中:
将其放入最初加载的视图中:
Put this in the controller:
Put this in the View that loads initially: