控制器类型“...”的当前操作请求在以下操作方法之间是不明确的

时间:2014-10-03 22:53:47

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

我无法解决这个问题。

控制器类型'ProductController'上的当前操作请求'ListProducts'在以下操作方法之间不明确: System.Web.Mvc.ActionResult类型Nettbutikk.Controllers.ProductController上的ListProducts(System.Nullable`1 [System.Int32]) System.Web.Mvc.ActionResult类型Nettbutikk.Controllers.ProductController上的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>

1 个答案:

答案 0 :(得分:0)

这是因为您有两个重载操作会导致路由混淆。在你的例子中,你想到了这个

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

将转到ListProducts(int id,string something)。没有!这是错误的假设....网址就像你的域/ 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);
        }