1

I am building a store locator in MVC. Here is my controller:

namespace IWOOv4.Controllers
{
  public class StoreListingController : Controller
  {
    // GET: StoreListing
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Details(string zip)
    {
        List<Site> result = SiteMgmt.GetByZip(zip);
        View().ViewData["sites"] = result;
        return View();
    }

    public ActionResult Details(string city, string st)
    {
        List<Site> result = SiteMgmt.GetByCity(city, st);
        View().ViewData["sites"] = result;
        return View();
    }
  }
}

Here is my View:

@{
  ViewBag.Title = "Home Page";
}

<div class="container">
  <div class="row">
    <!--HEADER-->
  </div>
  <div class="row sub-header">
    <span>ONLINE ORDERING</span>
  </div>
  <div class="row container-text">
    Find Your Store
  </div>
  <div class="row">
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Search" name="search">
      <div class="input-group-btn">        
        <button class="btn btn-default" type="button" id="searchbutton">
          <i class="glyphicon glyphicon-search"></i>
        </button>
      </div>
    </div>
  </div>
  <div class="row">
    <!--FOOTER-->
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function () {
    $('#searchbutton').on('click', function (event) {
      document.location = '@Url.Action("Details", "StoreListingController")';
    });
  });
</script>

When I am calling my Controller action, it does not display my store listing. It changes my URL from http://localhost/IWOOv4/Home/Index to http://localhost/IWOOv4/StoreListingController/Details. I looked up how to call a controller action to your view and the way I have it seems okay.. What am I doing wrong? Thanks so much!

Aries Azad
  • 346
  • 1
  • 4
  • 23

2 Answers2

0

Don't add the Controller suffix to the second parameter of Url.Action, MVC will do that for you when creating the route:

document.location = '@Url.Action("Details", "StoreListing")';
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I took the StoreListingController off as the second parameter and now when I search a zip code it gives the error code and changes the URL to http://localhost/IWOOv4/Home/Details. Is it suppose to change the URL? The Details action is suppose to list me out the stores once I search for zip or city. – Aries Azad Aug 07 '17 at 14:12
  • There's nothing in the code you've shown which would cause that behaviour. – Rory McCrossan Aug 07 '17 at 14:13
  • So could it be in the controller? I am just trying to refresh my memory of MVC. I haven't used it in over a year. I am so lost and I have spent hours trying to resolve this issue. I am at a stop halt with my task at this point. :( – Aries Azad Aug 07 '17 at 14:15
0

You can only have a maximum of 2 action methods with the same name on a controller, and in order to do that, 1 must be [HttpPost], and the other must be [HttpGet]

You might want to check the answers here:

Routing: The current request for action [...] is ambiguous between the following action methods

The current request for action [...] on controller type '...' is ambiguous between the following action methods