1. 程式人生 > >MVC之Ajax.BeginForm使用詳解之更新列表

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

分布 use html text col 返回 uno pts scripts

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

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

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

<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類型:

數據實體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:(部分代碼)

技術分享
      /// <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:

技術分享
@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" />
技術分享

如果你前面該引入的文件都引了,那麽在點擊查詢時,

div_uid 中就會顯示查詢到的ID

結果如下:

技術分享

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

註意:如果你有一個列表需要異步查詢並更新查詢結果,那就需要使用分布視圖!也就是說你還需要一個View才可以,不可以將結果直接返回到本頁!

控制器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>
}
技術分享

結果如下:

技術分享

點擊查詢後:

技術分享

作為筆記使用。無意侵權。

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