0

我正在尝试找出 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. 这个控制器是如何工作的,当我进行排序或搜索时,即使没有用于操作的控制器句柄,它也会正确返回。例子 :

    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

    即使我的控制器中没有sortand orderorcontains动作,这个动作也能很好地工作。

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

如果我知道没有问题。1个作品也许我可以弄清楚第2个问题。

4

1 回答 1

1
  1. 这个开源项目的完整源代码是可用的,所以如果你有耐心,你可以找到自己。基本上,通过Html.Grid(Model)在视图中执行,构造了一个新HtmlGrid的,它可以原始访问您的查询参数:

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

    所以这些不一定是路由属性。

  2. 您的 Ajax Check (" 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());
}
于 2018-08-01T08:50:32.213 回答