国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

淺析Asp.net MVC 中Ajax的使用

2024-09-01 08:28:25
字體:
來源:轉載
供稿:網友

一、使用System.Web.Mvc.Ajax

  1.1 System.Web.Mvc.Ajax.BeginForm

  1.2 System.Web.Mvc.Ajax.ActionLink

二、手工打造自己的“非介入式”Javascript”

一、使用System.Web.Mvc.Ajax

 

1.1 System.Web.Mvc.Ajax.BeginForm

     第一步:用Ajax.BeginForm創建Form

  @using (Ajax.BeginForm(    new AjaxOptions()    {      HttpMethod = "post",      Url = @Url.Action("Index","Reviews"),      InsertionMode = InsertionMode.Replace,      UpdateTargetId = "restaurantList",      LoadingElementId = "loding",      LoadingElementDuration = 2000    }))  {     <input type="search" name="searchItem"/>     <input type="submit" value="按名稱搜索"/>  }

       最終生成的form如下:

 <form id="form0" method="post"     data-ajax-url="/Reviews"    data-ajax-update="#restaurantList"    data-ajax-mode="replace"    data-ajax-method="post"    data-ajax-loading-duration="2000"    data-ajax-loading="#loding"    data-ajax="true"    action="/Reviews" novalidate="novalidate">

第二步:創建Ajax.BeginForm的new AjaxOptions()對象的Url指向的Action

 new AjaxOptions()    {       ...      Url = @Url.Action("Index","Reviews")         ...       }    public ActionResult Index(string searchKey = null)    {      var model = _restaurantReviews.Where(r => searchKey == null || r.Name.ToLower().Contains(searchKey.ToLower().Trim()))        .OrderByDescending(r => r.Rating)        .Take(100)        .Select(r=>new RestaurantReview()        {          City = r.City,          Country = r.Country,          Id = r.Id,          Name = r.Name,          Rating = r.Rating        }).ToList();      if (Request.IsAjaxRequest())      {        System.Threading.Thread.Sleep(1000 * 3);//模擬處理數據需要的時間        //return View(model)會返回整個頁面,所以返回部分視圖。        return PartialView("_RestaurantPatialView", model);      }      return View(model);    }

 注意:

    關于使用System.Web.Mvc.Ajax的說明:

       Controller的Action方法:

         (1)當顯式添加[HttpPost],傳給System.Web.Mvc.Ajax的AjaxOptions()的HttpMethod只能為 "post",

         (2)當顯式添加[HttpGet],傳給System.Web.Mvc.Ajax的AjaxOptions()的HttpMethod只能為 "get",

         (3)當都沒有顯式添加[HttpPost]和[HttpGet],傳給System.Web.Mvc.Ajax的AjaxOptions()的HttpMethod可以為 "get"也可以為"post",

      第三步:添加要承載更新頁面的html元素,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宿松县| 伊金霍洛旗| 翁牛特旗| 运城市| 白河县| 海林市| 泸州市| 中牟县| 白河县| 浦江县| 博湖县| 察雅县| 仁怀市| 肇东市| 昌乐县| 乌兰浩特市| 香港| 定安县| 锡林浩特市| 毕节市| 凤城市| 长兴县| 普兰县| 吴旗县| 隆林| 上蔡县| 沁源县| 大厂| 焦作市| 高要市| 勐海县| 太原市| 合作市| 车致| 舒兰市| 和硕县| 科技| 琼中| 玉龙| 酒泉市| 大庆市|