1. First, please set the following in the configuration file: (The item is present by default and True)
<key= "unobtrusivejavascriptenabled" value= "true"/>
2. Introduce the JS file in your _layout.cshtml:
<Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script><Scriptsrc= "~/scripts/jquery.validate.min.js"></Script><Scriptsrc= "~/scripts/jquery.validate.unobtrusive.min.js"></Script><Scriptsrc= "~/scripts/jquery.unobtrusive-ajax.min.js"></Script>
3. Get a simple value, such as a int,string type such as Id,name:
Data Entity User.cs:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacemvcapplication1.models{ Public classUser { Public intID {Get;Set; } Public stringName {Get;Set; } }}
Controller UserController.cs: (partial code)
/// <summary> ///defined user repositories/// </summary> PrivateList<user> _userrepository =NewList<user> { NewUser{id=1, name="AB"}, NewUser{id=2, name="BC"}, NewUser{id=3, name="CD"}, NewUser{id=4, name="Ace"} }; #regionGetUserID for (get a simple value) PublicActionResult UserID () {returnView (); } [HttpPost] Public intUserID (stringname) {User User= _userrepository.firstordefault (x = =string. Equals (X.name, Name, stringcomparison.currentcultureignorecase)); if(User = =NULL) { return-1; } returnuser.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")<Buttontype= "Submit"name= "CX"style= "width:80px; height:30px;">Query UserID</Button>}<DivID= "Div_uid"></Div><!--If it is asynchronous, the value entered in this text box will not be flushed out -<inputtype= "text"AutoComplete= "Off" />
If the file you introduced earlier is cited, then when you click on the query,
The ID that is queried is displayed in the Div_uid
The results are as follows:
4. Get a list of users to refresh the list asynchronously:
Note: If you have a list that requires an asynchronous query and updates the query results, you need to use the distribution view! In other words, you need a view to be able to return the results directly to this page!
Controller UserController.cs: (partial code)
#regionGetuserlist (get list of users for asynchronous refresh list)//GET:/user/ PublicActionResult userlist () {varresult =_userrepository; returnView (Result); } [HttpPost] PublicActionResult UserList (stringname) { varresult =_userrepository; if(!string. Isnullorwhitespace (name)) {result= _userrepository.where (U =u.name.contains (Name)). ToList (); } returnPartialview ("_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 = I Nsertionmode.replace}) {@Html. TextBox ("name")<Buttontype= "Submit"name= "CX"style= "width:80px; height:30px;">Query userlist</Button> }<Table> <thead> <TR> <TD>User ID</TD> <TD>User name</TD> </TR> </thead> <tbodyID= "TB">@Html. Partial ("_pview", Model)</tbody></Table><!--If it is asynchronous, the value entered in this text box will not be flushed out -<inputtype= "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>}
The results are as follows:
After clicking on the query:
5. Well, basically the mainstream of the 2 usage have, hope to be helpful to everyone!
MVC's Ajax.beginform Use a detailed list of updates