[英]JSON object from ASP.NET controller to view
I have a situation where a when a web page is accessed a controller action runs which retrieves the data for that page based on a user selection. 我有一种情况,当访问网页时,将运行控制器操作,该操作将根据用户选择检索该页面的数据。 I am attempting to send the data back to the page as a JSON object, however, the data opens up as one large string in an HTML page.
我试图将数据作为JSON对象发送回页面,但是,数据在HTML页面中作为一个大字符串打开。 The controller action, in a nutshell, looks like the following snippet:
简而言之,控制器动作类似于以下代码片段:
Public JsonResult MyMethod(string userSelection)
{
string userData = (string) Data;
return Json(userData, “text”, JsonRequestBehavior.AllowGet);
}
I first tried to use the JQuery $.getJson()
method but I think this is wrong as I believe it issues another call to the action method for the data, which is not what I want to do. 我首先尝试使用JQuery
$.getJson()
方法,但是我认为这是错误的,因为我认为它会向数据的action方法发出另一个调用,这不是我想要的。 What I want is to access the JSON object in JavaScript code so I can use the property data to populate fields on the web page. 我想要的是用JavaScript代码访问JSON对象,以便可以使用属性数据填充网页上的字段。 The basic question is what must I do in my JavaScript to receive the JSON object when the page is first rendered?
基本问题是首次呈现页面时,我必须在JavaScript中做什么才能接收JSON对象? I apologize if I am missing something fundamental;
如果我缺少基本要点,我深表歉意。 this is my first try.
这是我的第一次尝试。
I still had no luck today but when I left work I came up with a strategy walking to my car. 今天我仍然没有运气,但是当我离开工作时,我想到了一个步行去我车的策略。 A user makes a selection from a page that presents a list prior to entering the page on which I cannot figure out how to work with JsonResult.
用户在进入无法确定如何使用JsonResult的页面之前,从显示列表的页面中进行选择。 Part of the problem is the user selection contains a link that calls the controller/action that returns the JsonResult which conflicts with using $.getJson() within the page where I want to work with JsonResult.
问题的一部分是用户选择包含一个链接,该链接调用返回JsonResult的控制器/操作,该操作与我要使用JsonResult的页面中使用$ .getJson()发生冲突。 So here is my strategy: When the user makes the selection that brings them to the (problematic) page, I will call a controller/action that strictly works with (ASP) ViewData, and use the ViewData to initially present that page.
所以这是我的策略:当用户做出选择,将其带到(问题)页面时,我将调用一个严格与(ASP)ViewData配合使用的控制器/动作,并使用ViewData最初显示该页面。 Once on the page, the user can change the selection -- I will handle this with a JavaScript event that uses a $.getJason() call to a different controller/action method that works with (ASP) JsonResult.
进入页面后,用户可以更改选择-我将通过一个JavaScript事件处理该事件,该事件使用$ .getJason()调用来与(ASP)JsonResult一起使用的另一个控制器/操作方法。 After I try this strategy I shall post my results for whomever is interested.
在尝试了这种策略之后,我将把感兴趣的任何人发布我的结果。
You want parseJSON
not getJSON
您想要
parseJSON
而不是getJSON
http://api.jquery.com/jQuery.parseJSON/ http://api.jquery.com/jQuery.parseJSON/
Edit - Oh wait you are pointing your browser at the JsonResult
as if it was an ActionResult
? 编辑-哦,您是否将浏览器指向
JsonResult
,就好像它是ActionResult
? That is not going to work. 那是行不通的。
Render a proper view, and use getJSON
to call the JsonResult
action. 渲染适当的视图,并使用
getJSON
调用JsonResult
操作。
getJSON
is what you are looking for. getJSON
是什么 ,你所期待的。 Call that on the DOM ready event which will executes once the DOM finishes loading. 调用DOM ready事件,该事件将在DOM完成加载后执行。
$(function(){
//This code will be executed on the DOM ready ( when the page is loaded)
$.getJSON("YourControllerName/MyMethod?userSelection=someValue",function(data){
alert(data.FirstName);
alert(data.AnotherPropertyName);
});
});
getJSON
is a shorthand of jQuery ajax
method with datatype set as json
getJSON
是jQuery ajax
方法的简写,其数据类型设置为json
Assuming your JSON data you are retuning is something like this 假设您要重新调整的JSON数据是这样的
{
"FirstName": "Scott",
"AnotherPropertyName": "SomeValue"
}
To return data like above, change your Action method like this 要像上面那样返回数据,请像这样更改您的Action方法
public JsonResult MyMethod(string userSelection)
{
var result=new { FirstName="Scott", AnotherPropertyName="Great"};
return Json(result,JsonRequestBehavior.AllowGet);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.