简体   繁体   English

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

[英]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 在以下操作方法之间,当前对控制器类型“ 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)

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). 将转到ListProducts(int id,字符串的东西)。 No! 没有! that is wrong assumption....the url would be like yourdomain/1 because something is empty....which seem the first action. 这是错误的假设。...URL类似于yourdomain / 1,因为某些内容为空。...这似乎是第一个动作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在以下操作方法之间,当前对控制器类型“ AdminController”的操作“ AdminManagement”的请求不明确 - The current request for action 'AdminManagement' on controller type 'AdminController' is ambiguous between the following action methods 控制器类型&#39;WeeklyTargetController&#39;上的当前动作&#39;Create&#39;请求在以下操作方法之间是不明确的: - The current request for action 'Create' on controller type 'WeeklyTargetController' is ambiguous between the following action methods: 路由:当前的动作请求 […] 在以下动作方法之间是不明确的 - Routing: The current request for action […] is ambiguous between the following action methods 控制器类型{1}上的当前操作请求{0}不明确 - The current request for action {0} on controller type {1} is ambiguous 控制器类型为{x}的当前操作请求{x}模棱两可 - The current request for action {x} on controller type {x} is ambiguous MVC异常:以下操作方法之间存在歧义 - MVC Exception: Ambiguous between the following action methods 在以下操作方法之间请求歧义 - Request ambiguos between the following action methods Create 控制器中的MVC5模糊动作方法 - MVC5 ambiguous action methods in controller 控制器的动作在具有相同名称的基类的动作之间是不明确的。 - The controller's action is ambiguous between the action of the baseclass with same name. AmbiguousMatchException:行动要求不明确 - AmbiguousMatchException: Request for action is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM