【问题标题】:Can i join pagelist and search in one index? [duplicate]我可以加入页面列表并在一个索引中搜索吗? [复制]
【发布时间】:2018-06-04 19:08:19
【问题描述】:
    public ActionResult Index(string search = "")
    {
        var data = GetZamowienia(search);
        ViewBag.search = search;
        ViewBag.Pojazd = new SelectList((from p in db.Pojazd select new
        {
            ID = p.IDPojazdu,
            FullPojazd = p.Marka + " " + p.Model }),
            "ID",
            "FullPojazd",
            null);
        return View(data);
    }

    public ActionResult Index(int? page)
    {
        var zamowienie = db.Zamowienie;
        int maxRows = 1;
        IOrderedQueryable<Zamowienie> zamowienia = (from zam in db.Zamowienie
                                                    select zam)
        .OrderBy(prac => prac.IDPracownika);
        int pageNumber = (page ?? 1);
        return View(zamowienia.ToPagedList(pageNumber, maxRows));
    }

    public List<Zamowienie> GetZamowienia(string search)
    {
        {
            var v = (from a in db.Zamowienie
                     where
                             a.Pracownik.Imie.Contains(search)
                     select a
                            );
            return v.ToList();
        }
    }

这里是错误: 当前对控制器类型“ZamowienieController”的操作“索引”的请求在以下操作方法之间不明确: 类型 webrent.Controllers.ZamowienieController 上的 System.Web.Mvc.ActionResult Index(System.String) System.Web.Mvc.ActionResult Index(System.Nullable`1[System.Int32]) on type webrent.Controllers.ZamowienieController

我不知道如何同时制作有效的页面列表和搜索。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    错误消息告诉您,您有两个名称为 Index 的方法。

    您可以使用以下两种方法来解决此错误。

    方法一:一种多参数索引方法

    public ActionResult Index(string search = "", int? page = null)
    {
        // Your code here
    }
    

    在您的请求 URL 中填写您要使用的参数。

    例如:

    • myUrl.extension/controller/index?search=abcdef
    • myUrl.extension/controller/index?page=3
    • myUrl.extension/controller/index?search=abcdef&page=3

    方法 2:路线

    在您的方法上配置路由

    [Route("SearchByName"]
    public ActionResult Index(string search = "")
    {
       // Your code here
    }
    

    URL 现在应该如下所示: myUrl.extension/controller/SearchByName?search=abcdef

    [Route("GetByPage)]
    public ActionResult Index(int? page)
    {
        // Your code here
    }
    

    网址现在应该如下所示: myUrl.extension/controller/GetByPage?page=3

    【讨论】:

      猜你喜欢
      • 2011-07-12
      • 2020-12-04
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多