查看页面
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<input type="text" id="txtmasterserach" name="txtmasterserach" placeholder="City, region, district or specific hotel" autocomplete="off"/>
<input type="hidden" name="hidenkeyvalues" id="MovieID" />
<script type="text/javascript">
$(document).ready(function () {
$("#txtmasterserach").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Company/getautobox",
type: "POST",
dataType: "json",
data: { Prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return { value: item.Id, label: item.name };
}))
}
})
},
select: function (event, ui) {
$("#MovieID").val(ui.item.value);
$("#txtmasterserach").val(ui.item.label);
event.preventDefault();
return false;
},
focus: function (event, ui) {
$("#MovieID").val(ui.item.value);
// $("#txtmasterserach").val(ui.item.label);
event.preventDefault();
return false;
},
messages: {
noResults: "", results: ""
}
});
});
</script>
对于控制器:
public class companyController : Controller
{
public JsonResult getautobox(String Prefix)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
SqlCommand cmd = new SqlCommand("procedurename", con);
cmd.Parameters.AddWithValue("@prefix", Prefix);
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
List<autosearchdatalist> auto = new List<autosearchdatalist>();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
auto.Add(new autosearchdatalist
{
Id = ds.Tables[0].Rows[i]["Id"].ToString(),
name = ds.Tables[0].Rows[i]["hotelname"].ToString()
});
}
}
if (ds.Tables[1].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
auto.Add(new autosearchdatalist {
Id = ds.Tables[1].Rows[i]["Id"].ToString(),
name = ds.Tables[1].Rows[i]["hotelname"].ToString()
});
}
}
if (ds.Tables[2].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
{
auto.Add(new autosearchdatalist
{
Id = ds.Tables[2].Rows[i]["Id"].ToString(),
name = ds.Tables[2].Rows[i]["hotelname"].ToString()
});
}
}
String msg = "";
return Json(auto);
}
}
保持路由器设置默认,否则操作将不会调用
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "company", action = "Index", id = UrlParameter.Optional }
);
}
}