简体   繁体   中英

The current request for action […] on controller type '…' is ambiguous between the following action methods

I'm having trouble to solve this problem.

The current request for action 'ListProducts' on controller type 'ProductController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult ListProducts(System.Nullable`1[System.Int32]) on type Nettbutikk.Controllers.ProductController System.Web.Mvc.ActionResult ListProducts(Int32, System.String) on type Nettbutikk.Controllers.ProductController

Anyone out there who can help me?

Context:

    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;
    }

Controller:

    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);
    }

And the View:

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

This is because you have two overloaded actions which result in the confusion of the routing. In your example, you thought this

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

would go to the ListProducts(int id , string something). No! that is wrong assumption....the url would be like yourdomain/1 because something is empty....which seem the first action.

So when something variable is empty, the two action methods are confusion for the routing engine.

So change the names to be different

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);
    }

or just one action

  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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM