AutoComplete plug-in function is similar to Google Suggest function, can be in the user input query conditions, automatically prompt users a number of entries, convenient for users to quickly query. Below we describe how the next AutoComplete plugin is used under MVC.
First, you need to download the AutoComplete plugin, which is: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
Next, you need to introduce two files (except jquery files) on the page, respectively, Jquery.autocomplete.js and JQUERY.AUTOCOMPLETE.CSS, the code is as follows:
<script src= "@Url. Content (" ~/content/autocomplete/jquery.autocomplete.js ")" Type= "Text/javascript" ></ Script><link href= "@Url. Content (" ~/content/autocomplete/jquery.autocomplete.css ")" rel= "stylesheet" type= " Text/css "/>
The front-end page just needs to add a text box with the following code:
<input type= "text" name= "Higheruserid" id= "Higheruserid"/>
The JS code you need to write is as follows:
<script type= "Text/javascript" > $ (function () { $ ("#HigherUserID"). AutoComplete ("/user/ Getuserbyusername ", { dataType:" JSON ", minchars:0, width:200, showname:" Truename ", Selfaction:salesdeletename, cachelength:0, autofill:false, formatitem:function (row, I, max) { return i + "/" + Max + ": \" "+ row. Truename + "\" ["+ row. Usercode + "]"; }, formatresult:function (row) { return row. truename; } }). Result (function (event, row) { }); }) </script>
The background code retrieves the person information according to the condition, pagination display, returns to the front end a JSON data:
Public ActionResult Getuserbyusername (string q) { int _totalcount = 0; Queryuser query = new Queryuser (); Query.truename = q; Query.state = "0"; var userlist = userservice.getusersbypage (query, ten, 1, out _totalcount); ilist<userinfo> users = userlist. Tolist<userinfo> (); Return Json (Users, Jsonrequestbehavior.allowget); }
The use of AutoComplete plugins under MVC