Detailed explanation of how to use JsonResult
This article mainly introduces the detailed explanation of mvc's use of JsonResult to return Json data. It has certain reference value. Interested friends can refer to it. The following methods are defined in
controller:
public JsonResult UpdateSingle(int id, string actionName, string actionValue) { var res = new JsonResult(); //var value = "actionValue"; //db.ContextOptions.ProxyCreationEnabled = false; var list = (from a in db.Articles select new { name = a.ArtTitle, yy = a.ArtPublishTime }).Take(5); //记得这里要select new 否则会报错:序列化类型 System.Data.Entity.DynamicProxies XXXXX 的对象时检测到循环引用。 //不select new 也行的加上这句 //db.ContextOptions.ProxyCreationEnabled = false; res.Data = list;//返回列表 var name = "小华"; var age = "12"; var name1 = "小华"; var age1 = "12"; res.Data = new object[] { new { name, age }, new { name1, age1 } };//返回一个自定义的object数组 var person = new { Name = "小明", Age = 22, Sex = "男" }; res.Data = person;//返回单个对象; res.Data = "这是个字符串";//返回一个字符串,意义不大; res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。 return res; }
Page call:
<a href="javascript:void(0);" onclick="javascript:upclick(this);">Click Me</a> <script type="text/javascript"> function upclick(o) { var obj = $(o); alert(obj); $.ajax({ url: "/Articles/UpdateSingle?ran=" + Math.random(), type: "GET", dataType: "json", data: { id: obj.attr("id"), actionName: obj.attr("actionName"), actionValue: obj.attr("actionValue") }, success: function (data) { // if (data.result == "True") { // alert("修改成功!"); // } // if (obj.attr("actionName") == "ArtVerify") { // } $(o).html(data[0].name); obj.attr("actionValue", data[0].result); } }) } </script>
The above is used in mvc, how to use it in webform?
Reference Newtonsoft.Json.dll in the webform;
Of course, you can also splice the strings yourself.
protected void Page_Load(object sender, EventArgs e) { var customer = new customer { name = "李华", sex = "男" }; var customer1 = new customer { name = "小芳", sex = "女" }; var li = new List<customer>(); li.Add(customer); li.Add(customer1); var list = Newtonsoft.Json.JavaScriptConvert.SerializeObject(li); var tt = "[{\"name\":\"李华\",\"sex\":\"男\"},{\"name\":\"小芳\",\"sex\":\"女\"}]"; //new Newtonsoft.Json.JsonSerializer()..(customer); Response.Write(tt); Response.End(); } public class customer { public string name { get; set; } public string sex { get; set; } }
Page method:
<p> <a href="javascript:void(0)" onclick="javascript:getJsonData();">GetJsonData</a> </p> <p id="datap"> ggg </p> <script type="text/javascript"> function getJsonData() { var str = ""; $.getJSON("/Json.aspx", function (data) { var tt = ""; $.each(data, function (k, v) { $.each(v, function (kk, vv) { tt += kk + ":" + vv + "<br/>"; }); }); $("#datap").html(tt); }); } </script>
Display results:
Li Yanhui ASP basic video tutorial
The above is the detailed content of Detailed explanation of how to use JsonResult. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

The MVC architecture (Model-View-Controller) is one of the most popular patterns in PHP development because it provides a clear structure for organizing code and simplifying the development of WEB applications. While basic MVC principles are sufficient for most web applications, it has some limitations for applications that need to handle complex data or implement advanced functionality. Separating the model layer Separating the model layer is a common technique in advanced MVC architecture. It involves breaking down a model class into smaller subclasses, each focusing on a specific functionality. For example, for an e-commerce application, you might break down the main model class into an order model, a product model, and a customer model. This separation helps improve code maintainability and reusability. Use dependency injection

ASP built-in objects include Request, Response, Session, Application, Server, Session.Contents, Application.Contents, Server.CreateObject, Server.MapPath, Server.Execute, Server.Transfer, etc. Detailed introduction: 1. Request: represents HTTP request object, etc.

SpringMVC framework decrypted: Why is it so popular, specific code examples are needed Introduction: In today's software development field, the SpringMVC framework has become a very popular choice among developers. It is a Web framework based on the MVC architecture pattern, providing a flexible, lightweight, and efficient development method. This article will delve into the charm of the SpringMVC framework and demonstrate its power through specific code examples. 1. Advantages of SpringMVC framework Flexible configuration method Spr

The MVC (Model-View-Controller) pattern is a commonly used software design pattern that can help developers better organize and manage code. The MVC pattern divides the application into three parts: Model, View and Controller, each part has its own role and responsibilities. In this article, we will discuss how to implement the MVC pattern using PHP. Model A model represents an application's data and data processing. usually,

ASP development tools include Visual Studio, Dreamweaver, FrontPage, EditPlus, UltraEdit, SQL Server Management Studio, RAD Studio, Delphi, Asp.NET and Oracle SQL Developer.

ASP scanning tool vulnerability detection: 1. Select the appropriate scanning tool; 2. Configure the scanning target in the scanning tool; 3. Configure scanning options as needed; 4. After the configuration is completed, start the scanning tool to start scanning; 5. Scanning tool A report will be generated listing the detected vulnerabilities and security issues; 6. Fix the detected vulnerabilities and security issues according to the recommendations in the report; 7. After fixing the vulnerability, re-run the scanning tool to ensure that the vulnerability has been successfully exploited repair.

In Web development, MVC (Model-View-Controller) is a commonly used architectural pattern for processing and managing an application's data, user interface, and control logic. As a popular web development language, PHP can also use the MVC architecture to design and build web applications. This article will introduce how to use MVC architecture to design projects in PHP, and explain its advantages and precautions. What is MVCMVC is a software architecture pattern commonly used in web applications. MV
