Ajax 为搜索方法传递同一参数的多个值
我想更新我的搜索功能,这样它就可以接受额外的参数,这些参数将通过复选框(比如纽约、东京和柏林)以友好的方式构建。现在,我的控制器接受页码和搜索字符串,这是用Ajax调用的,用于内页分页 所以我的搜索链接现在是:/TopEventsul?searchString=HouseParty 并希望添加更多搜索功能,如:/TopEventsul?searchString=HouseParty&Location=London&Location=Tokio 你能给我指一下正确的方向吗?或者给我举几个例子 下面是我的控制器功能Ajax 为搜索方法传递同一参数的多个值,ajax,asp.net-mvc,entity-framework,linq,search,Ajax,Asp.net Mvc,Entity Framework,Linq,Search,我想更新我的搜索功能,这样它就可以接受额外的参数,这些参数将通过复选框(比如纽约、东京和柏林)以友好的方式构建。现在,我的控制器接受页码和搜索字符串,这是用Ajax调用的,用于内页分页 所以我的搜索链接现在是:/TopEventsul?searchString=HouseParty 并希望添加更多搜索功能,如:/TopEventsul?searchString=HouseParty&Location=London&Location=Tokio 你能给我指一下正确的方向吗?或者给我举几个例子 下面
// GET: Ul
public ActionResult Index(int? pageNum, string searchString)
{
pageNum = pageNum ?? 0;
ViewBag.IsEndOfRecords = false;
if (Request.IsAjaxRequest())
{
var customers = GetRecordsForPage(pageNum.Value);
ViewBag.IsEndOfRecords = (customers.Any()) && ((pageNum.Value * RecordsPerPage) >= customers.Last().Key);
return PartialView("_TopEventLi", customers);
}
else
{
LoadAllTopEventsToSession(searchString);
ViewBag.TopEvents = GetRecordsForPage(pageNum.Value);
return View("Index");
}
}
public void LoadAllTopEventsToSession(string searchString)
{
var students = from s in db.TopEvents
select s;
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.Location.Contains(searchString)
|| s.Title.Contains(searchString));
}
var customers = students.ToList();
int custIndex = 1;
Session["TopEventi"] = customers.ToDictionary(x => custIndex++, x => x);
ViewBag.TotalNumberCustomers = customers.Count();
}
public Dictionary<int, TopEvents> GetRecordsForPage(int pageNum)
{
Dictionary<int, TopEvents> customers = (Session["TopEventi"] as Dictionary<int, TopEvents>);
int from = (pageNum * RecordsPerPage);
int to = from + RecordsPerPage;
return customers
.Where(x => x.Key > from && x.Key <= to)
.OrderBy(x => x.Key)
.ToDictionary(x => x.Key, x => x.Value);
}
//GET:Ul
公共操作结果索引(int?pageNum,string searchString)
{
pageNum=pageNum±0;
ViewBag.IsEndOfRecords=false;
if(Request.IsAjaxRequest())
{
var客户=GetRecordsForPage(pageNum.Value);
ViewBag.IsEndOfRecords=(customers.Any())&&((pageNum.Value*RecordsPerPage)>=customers.Last().Key);
返回部分视图(“TopEventLi”,客户);
}
其他的
{
LoadAllTopEventsToSession(搜索字符串);
ViewBag.TopEvents=GetRecordsForPage(pageNum.Value);
返回视图(“索引”);
}
}
public void LoadAllTopEventsToSession(字符串搜索字符串)
{
var students=来自db.TopEvents中的s
选择s;
如果(!String.IsNullOrEmpty(searchString))
{
students=students.Where(s=>s.Location.Contains)(搜索字符串)
||包含(搜索字符串));
}
var customers=students.ToList();
int custIndex=1;
会话[“TopEventi”]=customers.ToDictionary(x=>custinex++,x=>x);
ViewBag.TotalNumberCustomers=customers.Count();
}
公共字典GetRecordsForPage(int pageNum)
{
字典客户=(会话[“TopEventi”]作为字典);
int from=(pageNum*RecordsPerPage);
int-to=from+RecordsPerPage;
回头客
.Where(x=>x.Key>from&&x.Key x.Key)
.ToDictionary(x=>x.Key,x=>x.Value);
}
问候 这里的位置是固定参数,所以您可以简单地执行以下操作
public ActionResult Index(int? pageNum, string searchString, IEnumerable<string> Location)
{
// Your code
}
//Ajax对索引的调用
function testAjax() {
$.ajax({
url: "@Url.Content("~/Your-Controller/Index")",
data: "pageNum=" + your data for pageNum + "&searchString=" + your searchString + "¶m=" + param,
dataType: "json",
type: "Post",
contentType: "application/json; charset=utf-8",
beforeSend: function () {
//Code for before send whatever you have to do.
},
success: function (data) {
//Your code when all ok
},
error: function (response) {
//Your code when error ocure
alert(response.responseText);
},
failure: function (response) {
//your code for failure
alert(response.responseText);
}
})
return false;
}
添加一个参数
IEnumerable Location
非常感谢您的帮助。我可以用Ajax调用这些搜索方法吗?这样页面就不会重新加载(它可以工作,但是页面会重新加载)?我已经从专业ASP.NETMVC5图书musicstore中写了一个表单,但是页面重新加载。。。我应该为这一部分打开一个新问题吗?不,你不需要再添加一个问题。这是部分调用。让我给你一个如何通过ajax调用这些方法的示例代码…给我更多的时间来创建这些类型的函数。谢谢:)期待
function testAjax() {
$.ajax({
url: "@Url.Content("~/Your-Controller/Index")",
data: "pageNum=" + your data for pageNum + "&searchString=" + your searchString + "¶m=" + param,
dataType: "json",
type: "Post",
contentType: "application/json; charset=utf-8",
beforeSend: function () {
//Code for before send whatever you have to do.
},
success: function (data) {
//Your code when all ok
},
error: function (response) {
//Your code when error ocure
alert(response.responseText);
},
failure: function (response) {
//your code for failure
alert(response.responseText);
}
})
return false;
}