MVC之Ajax.BeginForm使用詳解之更新列表

1.首先,請在配置文件設置以下:(該項默認都存在且爲true)html

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

2.在你的_layout.cshtml中引入JS文件:jquery

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

3.獲取簡單的某個值,好比ID,NAME等int,string類型:ajax

數據實體User.cs:異步

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

namespace MvcApplication1.Models
{
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

 

控制器UserController.cs:(部分代碼)ui

      /// <summary>
        /// 定義的用戶倉庫
        /// </summary>
        private List<User> _userRepository = new List<User> { 
        new User{ID=1,Name="ab"},
        new User{ID=2,Name="bc"},
        new User{ID=3,Name="cd"},
        new User{ID=4,Name="ace"}
        };
        #region GetUserID For (獲取簡單的某個值)
        public ActionResult UserID()
        {
            return View();
        }
        [HttpPost]
        public int UserID(string name)
        {
            User user = _userRepository.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase));
            if (user == null)
            {
                return -1;
            }
            return user.ID;
        }
        #endregion

視圖UserID.cshtml:spa

@using MvcApplication1.Models;
@model User
@{
    ViewBag.Title = "查詢用戶ID";
}
@using (Ajax.BeginForm("UserID", "User", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "div_uid", InsertionMode = InsertionMode.Replace }))
{
    @Html.TextBox("name")
    <button type="submit" name="CX" style="width:80px; height:30px;">查詢UserID</button>
}
<div id="div_uid"></div>
<!--若是是異步,則本文本框輸入的值不會被刷新掉-->
<input type="text" autocomplete="off" />

若是你前面該引入的文件都引了,那麼在點擊查詢時,code

div_uid 中就會顯示查詢到的ID

結果以下:orm

 

4.獲取用戶列表,用於異步刷新列表:htm

注意:若是你有一個列表須要異步查詢並更新查詢結果,那就須要使用分佈視圖!也就是說你還須要一個View才能夠,不能夠將結果直接返回到本頁!blog

控制器UserController.cs:(部分代碼)

     #region GetUserList  (獲取用戶列表,用於異步刷新列表)
        // GET: /User/
        public ActionResult UserList()
        {
            var result = _userRepository;
            return View(result);
        }
        [HttpPost]
        public ActionResult UserList(string name)
        {
            var result = _userRepository;
            if (!string.IsNullOrWhiteSpace(name))
            {
                result = _userRepository.Where(u => u.Name.Contains(name)).ToList();
            }
            return PartialView("_pview", result);
        }
        #endregion

主視圖UserList.cshtml:

@using MvcApplication1.Models;
@model List<User>
@{
    ViewBag.Title = "Index";
}
@using (Ajax.BeginForm("UserList", "User", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "tb", InsertionMode = InsertionMode.Replace }))
{
    @Html.TextBox("name")
     <button type="submit"  name="CX" style="width:80px; height:30px;">查詢UserList</button> 
}
<table>
    <thead>
        <tr>
            <td>用戶ID</td>
            <td>用戶名稱</td>
        </tr>
    </thead>
    <tbody id="tb">
    @Html.Partial("_pview", Model)
    </tbody>
</table>
<!--若是是異步,則本文本框輸入的值不會被刷新掉-->
<input type="text" autocomplete="off" />

分佈視圖_pview.cshtml:

@using MvcApplication1.Models;
@model List<User>
@{
    Layout = null;
    ViewBag.Title = "_pview";
}
@foreach (User u in Model)
{
    <tr>
        <td>@u.ID</td>
        <td>@u.Name</td>
    </tr>
}

結果以下:

點擊查詢後:

5.好了,基本上主流的2個用法都有,但願能對你們有幫助!

相關文章
相關標籤/搜索