MVC视图调用控制器方法不正确

时间:2022-12-01 23:21:02

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

我正在MVC中构建一个商店定位器。这是我的控制器:

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!

当我调用我的Controller操作时,它不会显示我的商店列表。它将我的URL从http:// localhost / IWOOv4 / Home / Index更改为http:// localhost / IWOOv4 / StoreListingController / Details。我抬头看了如何调用控制器动作到你的视图和我的方式似乎没关系..我做错了什么?非常感谢!

2 个解决方案

#1


0  

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

不要将Controller后缀添加到Url.Action的第二个参数,MVC将在创建路径时为您执行此操作:

document.location = '@Url.Action("Details", "StoreListing")';

#2


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]

在控制器上最多只能有2个具有相同名称的操作方法,为了做到这一点,1必须是[HttpPost],另一个必须是[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

控制器类型“...”的当前操作请求在以下操作方法之间是不明确的

#1


0  

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

不要将Controller后缀添加到Url.Action的第二个参数,MVC将在创建路径时为您执行此操作:

document.location = '@Url.Action("Details", "StoreListing")';

#2


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]

在控制器上最多只能有2个具有相同名称的操作方法,为了做到这一点,1必须是[HttpPost],另一个必须是[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

控制器类型“...”的当前操作请求在以下操作方法之间是不明确的