MVC 5 中的搜索参数

Search parameter in MVC 5

我正在尝试找出 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; }
}

控制器是这样的:

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));
    }
}

我想把这个分成两个问题:

  1. 这个控制器是如何工作的,当我进行排序或搜索时,它 returns 正确,即使没有用于操作的控制器句柄。示例:

    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

    即使我的控制器中没有 sortordercontains 动作,这个动作也能正常工作。

  2. 很遗憾,GlobalSearch 操作 search 中的一项没有正常工作。它 returns 所有数据,无论我输入什么。例子 : http://localhost/MasterCustomers?search=sdfasdfasdfasdfsadwrh2w3rwegaweg&_=1533109639344

如果我知道怎么问就不会了。 1 成功了,也许我可以弄清楚第 2 题中的问题。

  1. 构造了full source code is available for this Open Source Project, so if you have some patience, you could find out yourself. Basically, by executing Html.Grid(Model) in the View, a new HtmlGrid,它可以原始访问您的查询参数:

    grid.Query = new NameValueCollection(grid.ViewContext.HttpContext.Request.QueryString);
    

    因此这些不必是路由属性。

  2. 您的 Ajax 检查 ("if (HttpContext.Request.Headers["X-...") 似乎不正确,您从哪里得到的?您提供的页面上的实施示例明显不同。通过调用 Index 而不是预期的 IndexGrid,您将丢失搜索参数

将您的 index 更改为:

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());
}