我正在尝试找出 ASP.Net MVC 5 中名为 MVC.Grid 的包之一.
我有如下所示的模型:
public class MasterCustomer
{
public System.Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
Controller 是这样的:
public class MasterCustomersController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: MasterCustomers
public ActionResult Index()
{
if (HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
return PartialView("_IndexGrid", db.MasterCustomers.ToList());
return View(db.MasterCustomers.ToList());
}
[HttpGet]
public PartialViewResult IndexGrid(String search)
{
return PartialView("_IndexGrid", db.MasterCustomers.Find(search));
}
}
我想将其分为两个问题:
这个 Controller 是如何工作的,当我进行排序或搜索时,即使没有该操作的 Controller 句柄,它也会正确返回。示例:
http://localhost/MasterCustomers?search=&sort=code&order=asc&_=1533109639307
http://localhost/MasterCustomers?search=&sort=code&order=asc&code-contains=tes&code-op=&code-contains=&_=1533109639308
即使我的 Controller 中没有
sort
和order
或contains
操作,此操作也运行良好。可悲的是 GlobalSearch 之一操作
search
无法正常工作。无论我输入什么,它都会返回所有数据。例子 :http://localhost/MasterCustomers?search=sdfasdfasdfasdfsadwrh2w3rwegaweg&_=1533109639344
如果我知道如何提问。 1 可行,也许我可以解决 2 中的问题。
最佳答案
full source code这个开源项目可用,所以如果你有耐心,你可以自己找到答案。基本上,通过执行
Html.Grid(Model)
在 View 中,一个新的HtmlGrid
已构建,它可以原始访问您的查询参数:grid.Query = new NameValueCollection(grid.ViewContext.HttpContext.Request.QueryString);
所以这些不一定是路由属性。
您的 Ajax Check ("
if (HttpContext.Request.Headers["X-...
") 似乎不正确,您从哪里得到它?上的实现示例您提供的页面明显不同。通过调用Index
而不是应有的IndexGrid
,您会丢失搜索参数
将您的索引
更改为:
public ActionResult Index()
{
return View();
}
和IndexGrid
到:
[HttpGet]
public PartialViewResult IndexGrid(String search)
{
if (String.IsNullOrEmpty(search))
return PartialView("_IndexGrid", db.MasterCustomers.ToList());
else
return PartialView("_IndexGrid", db.MasterCustomers.Where(x => x.Code.Contains(search) || x.Name.Contains(search)).ToList());
}
关于javascript - MVC 5 中的搜索参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51628345/