从外部JavaScript文件访问ASP.NET MVC模型数据 [英] Accessing ASP.NET MVC Model Data from external Javascript file

查看:210
本文介绍了从外部JavaScript文件访问ASP.NET MVC模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery的$ .getJSON从控制器访问模型数据,以及构建一个JSON对象在JavaScript作为用户添加和删除从列表中的项目使用。所有的JS在外部文件来完成。命中到服务器进行模型数据是在第一次加载页面后,只进行一次这样我就可以得到供用户选择选项的完整列表。在此之后,用户可以修改名单,他希望(和JSON对象进行相应的更新)为多,然后将其保存关闭(被写回到DB)。

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).

但这里是我的问题:当使用$ .getJSON,方法我需要在控制器调用需要一个ID,以便它可以查找什么特殊的资源我谈论并取回的,涉及到的选项列表该特定的资源。但是,从外部JS文件,我不知道如何获取ID传递给控制器​​。它的建成将打开摆在首位的页面,并坐在网址,但一旦页面加载,我不知道怎么才能把它传递给控制器​​获得这个标识的链接。我贴我的一些code以下的,因为我知道这是令人困惑的:

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:

在控制器详细方法,最初加载页面(需要RESOURCEID知道加载):

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");
}

在我的外部JS文件,我这样做是:

In my external JS file, I'm doing this:

$.getJSON("/Evaluation/PopulateJournalOptions/" + id, populateJSON);

和在控制器中,我写了被调用来构建JSON对象的另一种方法叫做PopulateJournalOptions:

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);
}

此外,我的问题是,一旦页面加载,我没有获得我需要传递给PopulateJournalOptions的ID。我试过只是没有经过任何PopulateJournalOptions,并创建一个新的对象那里设置它等于(评价)this.Model ,但this.Model显示为空值,可能是因为它的控制器的新实例一旦它从JS调用。所以我有点坚持在这里。任何想法?

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?

推荐答案

你有没有想过把ID为一个隐藏字段在页面上,然后让你的JavaScript阅读领域的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?

$.getJSON("/Evaluation/PopulateJournalOptions/" + $('#Id').attr('value'), populateJSON);

然后,在你看来,

Then, in your view,

<%= Html.Hidden("Id") %>

使用让您的模型所需的编号作为一个属性

With your model having the required Id as a property

这篇关于从外部JavaScript文件访问ASP.NET MVC模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆