你的位置:首页 > ASP.net教程

[ASP.net教程]MVC,在视图中直接请求Action?


@{  ViewBag.Title = "Index";}@section scripts{  <script type="text/javascript">    window.onload = function () {      //获取按钮,为其添加一个点击方法      document.getElementById("btnLoadPart").onclick = function () {        //1.创建一个异步对象        var xhr = new //2.调用        xhr.open("get", "/Part/_PartialTest", true);        //因为是get方式的异步请求,我们为了不使用缓存,就使用这句        //第二个参数随便给一个值        xhr.setRequestHeader("If-Modified-Since", 0)        //3。设置        xhr.onreadystatechange = function () {          if (xhr.readyState == 4 && xhr.status == 200)          {            //取出数据            var xhrNew = xhr.responseText;            //取玩数据,放到DIV中            document.getElementById("div1").innerHTML = xhrNew;          }        };        //发送        xhr.send(null);      };    };  </script>  }<h2>Index</h2><div id="div1"></div><input type="button" value="请求分部视图" id="btnLoadPart"/><div id="div2">  @{Html.RenderAction("_PartialTest");}</div>

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MVCStatrApp.Controllers{  public class PartController : Controller  {    // GET: Part    public ActionResult Index()    {      return View();    }    /// <summary>    /// 分部视图方法    /// </summary>    /// <returns></returns>    public ActionResult _PartialTest()    {      return PartialView();    }     }}