简体   繁体   English

可以从ASP.NET控制器查看JSON对象

[英]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.

相关问题 asp.net mvc将Json对象从视图传递到控制器 - asp.net mvc pass Json object from view to controller ASP.NET:开机自检列表 <Object> /使用Ajax(JSON)从视图到控制器进行建模 - ASP.NET: POST List<Object> / Model with Ajax (JSON) from view to controller 从控制器返回Json数据以查看ASP.NET MVC - Returning Json Data From Controller to View ASP.NET MVC 将 JSON 从视图发送到控制器 ASP.NET MVC 给出空值 - Sending JSON from View to Controller ASP.NET MVC gives null values Asp.Net Core - 选中复选框后将数组值从 Controller 传递给 Json 查看? - Asp.Net Core - pass array value from Controller to view by Json after checkbox is checked? 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC 在asp.net core 2.2中从视图到控制器发布位于视图模型内的对象列表 - Posting a list of object which is located inside a viewmodel from view to controller in asp.net core 2.2 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? javascript-如何将JSON对象中的数组从JavaScript传递给asp.net MVC控制器方法? - How to pass array in a JSON object from javascript to asp.net mvc controller method? 如何从ASP.NET MVC中的控制器传递JSON中的chart.js图表​​数据对象 - How to pass a chart.js chart data object in json from a controller in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM