javascript - MVC 5 中的搜索参数

标签 javascript c# asp.net-mvc asp.net-mvc-5 mvcgrid.net

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

我想将其分为两个问题:

  1. 这个 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 中没有 sortordercontains 操作,此操作也运行良好。

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

如果我知道如何提问。 1 可行,也许我可以解决 2 中的问题。

最佳答案

  1. full source code这个开源项目可用,所以如果你有耐心,你可以自己找到答案。基本上,通过执行 Html.Grid(Model)在 View 中,一个新的HtmlGrid已构建,它可以原始访问您的查询参数:

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

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

  2. 您的 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/

相关文章:

c# - C# 中的 MYSQL Entity Framework 连接器

c# - 与 ElasticSearch 相比,MongoDB 仅作为 NoSQL 数据库提供了哪些优势

c# - 将按钮插入 NavigationViewItem UWP

c# - 文件上传返回null

javascript - 如何使用 svg 用纹理填充多边形?

javascript - jquery 使用滚动删除类类型

javascript - AmCharts 4 - 如何在 MapPolygonSeries() 中使用自定义 ajax 响应

javascript - 在 iOS 中将字符串传递给 JavaScript 无法获取调用的函数

asp.net-mvc - MVC3 - 如何正确使用@html.checkbox?

javascript - 提交按钮重定向到另一个 View ASP.NET MVC