如何从asp.net MVC 4中的按钮单击调用控制器

Nur*_*ihu 14 c# asp.net asp.net-mvc asp.net-mvc-4

请我在MVC网站上工作,我有一个搜索页面和索引页面上的另一个搜索表单.当我从索引页面单击搜索按钮时,我想调用相同的搜索页面控制器.下面是我的按钮在索引页面上的方式.

    <span class="input-group-btn">
      <button class="btn btn-info" type="button" id="addressSearch"   
          onclick="location.href='<%: @Url.Action("List", "Search") %>'">
     Search</button></span>
Run Code Online (Sandbox Code Playgroud)

列表是我的搜索搜索页面中的操作,搜索是控制器名称.当我单击上面的按钮时,它将返回表单中的url

http:// localhost:52050 / <%:%20/Search/List%20%>

显示不良请求.我怀疑它来自我的路由,我不知道如何实现这一点,请任何帮助将不胜感激.

以下是我的路由方式

 routes.MapRoute(
                name: null,
                url: "Page{page}",
                defaults: new { Controller = "Search", action = "List" }
                );


            routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
            );
Run Code Online (Sandbox Code Playgroud)

Ehs*_*jad 31

你正在混合razoraspx语法,如果你的视图引擎是剃刀,只需这样做:

<button class="btn btn-info" type="button" id="addressSearch"   
          onclick="location.href='@Url.Action("List", "Search")'">
Run Code Online (Sandbox Code Playgroud)


Kin*_*hil 8

试试这个:

@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
Run Code Online (Sandbox Code Playgroud)

在你的代码应该是,

@Html.ActionLink("Search", "List", "Search", new{@class="btn btn-info", @id="addressSearch"})
Run Code Online (Sandbox Code Playgroud)