簡體   English   中英

控制器類型為{x}的當前操作請求{x}模棱兩可

[英]The current request for action {x} on controller type {x} is ambiguous

這是完整的錯誤:

The current request for action 'Index' on controller type 'ClientController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(System.String) on type MVCTest.Controllers.ClientController
System.Web.Mvc.ActionResult Index() on type MVCTest.Controllers.ClientController

對MVC來說是非常新的東西,在嘗試將搜索欄應用於數據表時,我一直收到此錯誤。

控制器:

    public ActionResult Index(string SearchString)
    {
        var Client = from c in db.Clients

                     select c;

        if (!String.IsNullOrEmpty(SearchString))
        {
            Client = Client.Where(s => s.Name.Contains(SearchString));
        }

        return View(Client);
    }

HTML:

<p>
@Html.ActionLink("Create New", "Create")

@using (Html.BeginForm())
{
<p>
    Title: @Html.TextBox("SearchString") <br />
    <input type="submit" value="Filter" />
</p>
}

任何人都知道如何解決這個問題,我已經困惑了一段時間。

用屬性裝飾您的動作以告知是采取行動還是采取行動:

[HttpGet]
  public ActionResult Index()
  {

        return View();
  }  

  [HttpPost]
  public ActionResult Index(string SearchString)
  {
        var Client = from c in db.Clients

                     select c;

        if (!String.IsNullOrEmpty(SearchString))
        {
            Client = Client.Where(s => s.Name.Contains(SearchString));
        }

        return View(Client);
   }

您可能錯過了帶有字符串參數的[HttpPost]屬性:

通常,您希望您的Index()操作方法返回一個視圖以響應GET請求。 該視圖呈現了一個表單,該表單將發布到稱為Index的操作方法。 您希望它以一個字符串參數結束。

ASP.NET不知道使用兩種方法中的哪一種。 [HttpPost]屬性告訴它將一個用於POST,將另一個用於GET。

暫無
暫無

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

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