首页 > Web开发 > 详细

mvc return json

时间:2014-03-04 00:49:53      阅读:530      评论:0      收藏:0      [点我收藏+]
1.控制器

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

2.页面视图
<%@ 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>

    <h2>
        GetEmployee</h2>
    <input type="button" value="Invoke" id="bt" />
    <div id="info">
    </div>
</asp:Content>

http://www.cnblogs.com/chenxizhang/archive/2010/01/31/1660601.html

mvc return json,布布扣,bubuko.com

mvc return json

原文:http://www.cnblogs.com/Rikybk/p/3578283.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!