There are many common examples. This article is just my work record.
Front-end page:
@{
Viewbag. Title = "Homepage ";
}
@ Section header
{
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ("# Keyword"). AutoComplete ({
Source: function (request, response ){
$. Ajax ({
URL: "@ URL. Action (" Search "," home ")", // The action to query
Type: "Post ", // Post submission
Datatype: "JSON ", // JSON format data. The default value is text.
Data: {Keyword: request. Term }, // Parameter. Why? Please advise
// Data: {Keyword: $ ("# keyword"). Val ()}, you can also
Success: function (data ){
Response ($. Map (data, function (item ){
Return {label: item. Text, value: item. Value };// Assign the returned jsonresult attribute to AutoComplete item
}));
}
});
},
Focus: function (event, UI ){
$ ('# Keyword'). Val (ui. item. Label ); // Select the text of the item
Return false;
},
Select: function (event, UI ){
$ ('# Keyword'). Val (ui. item. Label );// Select the text of the item
$ ('# Txtselectvalue'). Val (ui. item. value );// Select the item Value
Return false;
}
});
});
</SCRIPT>
}
Keywords:
@ Html. Textbox ("keyword ","")
@ Html. Hidden ("txtselectvalue ")// Intelligently query the selected value
ActionCode:
// Get:/home/Index
// Post:/home/search
Public actionresult index ()
{
Return view ();
}
[Httppost]
Public actionresult search (string keyword)
{
// Query by keyword and return a set of objects in JSON format
Method 1: loop
List <Object> items = new list <objects> ();
VaR item = new {text = "", value = ""}
Items. Add (item );
Method 2: directly add the object using LINQ to entity
VaR datalist = organservice. adddatalist (); // obtain the data list
Items. addrange (datalist. Select (O => New
{
TEXT = O. Name,
Value = O. Code. tostring ()
}));
//---------------------------------------
Return JSON (items, jsonrequestbehavior. allowget );
}