对控制器类型';..执行操作[..]的当前请求';在以下操作方法之间不明确

本文关键字:请求 操作方法 不明确 之间 执行 类型 控制器 操作 | 更新日期: 2023-09-27 18:25:34

我很难解决这个问题。

对控制器类型"ProductController"的操作"ListProducts"的当前请求在以下操作方法之间不明确:类型Nettbutikk.Controllers.ProductController上的System.Web.Mvc.ActionResult ListProducts(System.Nullable `1[System.Int32])类型Nettbutikk.Controllers.ProductController 上的System.Web.Mvc.ActionResult ListProducts(Int32,System.String)

有谁能帮我吗?

上下文:

    public List<Product> getAll(int id, string something)
    {
        var db = new DatabaseContext();
        List<Product> allProducts = new List<Product>();
        var products = db.Products.Where(p => p.SubCategoriesId == id).ToList();
        foreach (var p in products)
        {
            var product = new Product()
            {
                itemnumber = p.Id,
                name = p.Name,
                description = p.Description,
                price = p.Price,
                volum = p.Volum,
                producer = p.Producers.Name,
                pricePerLitre = pricePerLitre(p.Price, p.Volum),
                category = p.SubCategories.Categories.Name,
                subCategory = p.SubCategories.Name,
                country = p.Countries.Name
            };
            allProducts.Add(product);
        }
        return allProducts;
    }

控制器:

    public ActionResult ListProducts(int? id)
    {
        var db = new DBProduct();
        List<Product> listOfProducts;
        listOfProducts = db.getAll(id);
        return View(listOfProducts);
    }
    public ActionResult ListProducts(int id, string something)
    {
        var db = new DBProduct();
        List<Product> listOfProducts;
        listOfProducts = db.getAll(id,tull);
        return View(listOfProducts);
    }

观点:

    <a href='@Url.Action("ListProducts", "Product", new { id = 1, tull = "" })'>sub category</a>

对控制器类型';..执行操作[..]的当前请求';在以下操作方法之间不明确

这是因为您有两个重载操作,导致路由混乱。在你的例子中,你认为这个

 <a href='@Url.Action("ListProducts", "Product", new { id = 1, tull = "" })'>sub category</a>

将转到ListProducts(int id,string之类的)。不那是错误的假设。。。。url会像你的域/1,因为有些东西是空的。。。。这似乎是第一个动作。

因此,当某个变量为空时,这两种操作方法对路由引擎来说是混淆的。

因此,将名称更改为不同的

public ActionResult ListProductsbyId(int? id)
    {
        var db = new DBProduct();
        List<Product> listOfProducts;
        listOfProducts = db.getAll(id);
        return View(listOfProducts);
    }
    public ActionResult ListProductsByIdAndTull (int id, string tull)
    {
        var db = new DBProduct();
        List<Product> listOfProducts;
        listOfProducts = db.getAll(id,tull);
        return View(listOfProducts);
    }

或仅一个动作

  public ActionResult ListProducts(int? id, string tull)
        {
            var db = new DBProduct();
            List<Product> listOfProducts;
            if(String.IsNullOrEmpty(tull)
              {
                listOfProducts = db.getAll(id);
              }
            else
             {
             listOfProducts = db.getAll(id,tull);
             }
            return View(listOfProducts);
        }