user3849390
user3849390

Reputation: 13

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

This is the full error:

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

Very very new to MVC, and I keep getting this error while trying to apply a search bar to a table of data.

Controller:

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

Anyone know how to fix this I've been confused for a while now.

Upvotes: 1

Views: 774

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Decorate your actions with attribute to tell is it get action or post action:

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

Upvotes: 1

slippyr4
slippyr4

Reputation: 862

Probably you missed [HttpPost] attribute from the one with the string parameter:

Typically, you would be wanting your Index() action method to return a view in response to a GET request. That view renders a form which will POST to an action method called Index. You want that to end up at the one with a string parameter.

ASP.NET doesn't know which of the two methods to use. The [HttpPost] attribute tells it to use one for POST, and the other one for GET.

Upvotes: 0

Related Questions