簡體   English   中英

在以下操作方法之間,當前對控制器類型“ ...”的操作請求[…]不明確

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

我無法解決此問題。

在以下操作方法之間,當前對控制器類型“ ProductController”的操作“ ListProducts”的請求是模棱兩可的:類型為Nettbutikk.Controllers.ProductController的System.Web.Mvc.ActionResult ListProducts(System.Nullable`1 [System.Int32]) Nettbutikk.Controllers.ProductController類型的.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,字符串的東西)。 沒有! 這是錯誤的假設。...URL類似於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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM