微软自带的异步Ajax请求

一、使用步骤

二、示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication3.Controllers
{
    public class MSAjaxController : Controller
    {
        // GET: MSAjax
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetPrices(int id)
        {
            string result = (id*10)+"";
            System.Threading.Thread.Sleep(2000);

            return Content(result);
        }
    }
}
View Code
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        function success(data) {
            //alert(data);
        };
    </script>
</head>
<body>
    <div> 
        @using (Ajax.BeginForm("GetPrices", "MSAjax", new AjaxOptions()
        {
            Confirm = "确定获取吗?",
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,
            LoadingElementId = "loadingImg",
            UpdateTargetId = "lblPrice",
            OnSuccess = "success"
        }))
        {
            <div>
                请输入Id号:<input type="text" name="id" /><br />
                <input type="submit" value="获取价格" /><br />
                价格:<label id="lblPrice"></label>
                <div id="loadingImg" style="display:none">
                    <img src="~/Content/ico_loading.gif" />
                </div>
            </div>
        }
    </div>
</body>
</html>
View Code
原文地址:https://www.cnblogs.com/shaomenghao/p/4020236.html