[英]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.