AJAX, JSON, and MVC

Source: Internet
Author: User

There are several special features

1. the MVC Framework contains a special JSONActionResult, which can directly return a JSON object. Note that its format is different from the previous asmx and static page methods. It is a JSON object directly.

2. programming on the server and client is relatively simple. The server does not require explicit serialization, and the client does not need to parse the JSON string, because the returned result is a JSON object.

 

Part 1: Design in Controller

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication1.Controllers{    public class Employee    {        public int Id { get; set; }        public string Name { get; set; }    }    [HandleError]    public class HomeController : Controller    {        public ActionResult Index()        {            ViewData["Message"] = "Welcome to ASP.NET MVC!";            return View();        }        public ActionResult About()        {            return View();        }        public ActionResult Employee() {            return View();        }        [HttpPost]        public ActionResult GetEmployee() {            return Json(new Employee()            {                Id = 1,                Name = "chenxizhang"            });        }                    }}

Part 2: Design in View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">    GetEmployee</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    <script src="../../Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>    <script type="text/javascript" language="javascript">        $(function() {            $("#bt").click(function() {                $.ajax({                    type: "POST",                    contentType: "application/json",                    url: "http://localhost:44203/Home/GetEmployee",                    data: "{}",                    dataType: 'json',                    success: function(result) {                        alert(result.Id);                    }                });            });        });        </script>    
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.