【问题标题】:The current request for action [...] on controller type '...' is ambiguous between the following action methods当前对控制器类型“...”的操作请求 [...] 在以下操作方法之间不明确
【发布时间】:2014-10-03 22:53:47
【问题描述】:

我无法解决这个问题。

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

有谁可以帮助我吗?

上下文:

    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>

【问题讨论】:

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


    【解决方案1】:

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

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

    会去 ListProducts(int id , string something)。不!这是错误的假设....网址会像 yourdomain/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);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多