Updated list of Ajax. BeginForm in MVC
1. first, set as follows in the configuration file: (this item exists by default and is true) <add key = "UnobtrusiveJavaScriptEnabled" value = "true"/> 2. in your _ layout. introduce JS files in cshtml: <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. Get a simple value, such as ID, NAME, int, string type: data entity 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; } }}
Controller UserController. cs: (part of the Code)
/// <Summary> /// defined User repository // </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 (get a simple value) 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
View UserID. cshtml:
@ Using MvcApplication1.Models; @ model User @ {ViewBag. title = "query user 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; "> query UserID </button >}< div id =" div_uid "> </div> <! -- If it is asynchronous, the value entered in this text box will not be refreshed --> <input type = "text" autocomplete = "off"/>
If all the files you introduced earlier are cited, the queried ID is displayed in div_uid when you click query. The result is as follows: 4. obtain the user list for asynchronous refresh list. Note: If you have a list that needs to be queried asynchronously and updated, you need to use the distribution view! That is to say, you still need a View. You cannot directly return the result to this page! Controller UserController. cs: (part of the Code)
# Region GetUserList (GET User list for asynchronous refresh list) // 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
Main view 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; "> query UserList </button >}< table> <thead> <tr> <td> User ID </td> <td> User Name </td> </tr> </thead> <tbody Id = "tb"> @ Html. Partial ("_ pview", Model) </tbody> </table> <! -- If it is asynchronous, the value entered in this text box will not be refreshed --> <input type = "text" autocomplete = "off"/>
Distribution VIEW _ 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>}