下列类型的操作方法之间当前对控制器类型"..."的操作请求不明确:unesdoc.unesco.org unesdoc.unesco.org [英] The current request for action [...] on controller type '...' is ambiguous between the following action methods

查看:243
本文介绍了下列类型的操作方法之间当前对控制器类型"..."的操作请求不明确:unesdoc.unesco.org unesdoc.unesco.org的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决此问题.

在以下操作方法之间,当前对控制器类型"ProductController"的操作"ListProducts"的请求是模棱两可的: Nettbutikk.Controllers.ProductController类型的System.Web.Mvc.ActionResult ListProducts(System.Nullable`1 [System.Int32]) Nettbutikk.Controllers.ProductController类型上的System.Web.Mvc.ActionResult ListProducts(Int32,System.String)

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?

上下文:

    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>

推荐答案

这是因为您有两个重载的操作,这些操作导致路由混乱.在您的示例中,您认为这样

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>

将转到ListProducts(int id,用字符串输入).不!这是错误的假设....URL就像yourdomain/1,因为某些内容为空....这似乎是第一个动作.

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.

因此将名称更改为其他

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

这篇关于下列类型的操作方法之间当前对控制器类型"..."的操作请求不明确:unesdoc.unesco.org unesdoc.unesco.org的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆