如何从 asp.net MVC 4 中的按钮单击调用控制器
How to call controller from the button click in 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>
List 是我在搜索页面中的搜索操作,Search 是控制器名称。当我点击上面的按钮时,它 returns 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
}
);
您正在混合使用 razor 和 aspx 语法,如果您的视图引擎是 razor这样做:
<button class="btn btn-info" type="button" id="addressSearch"
onclick="location.href='@Url.Action("List", "Search")'">
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
在你的代码中应该是,
@Html.ActionLink("Search", "List", "Search", new{@class="btn btn-info", @id="addressSearch"})
拜托,我正在 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>
List 是我在搜索页面中的搜索操作,Search 是控制器名称。当我点击上面的按钮时,它 returns 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
}
);
您正在混合使用 razor 和 aspx 语法,如果您的视图引擎是 razor这样做:
<button class="btn btn-info" type="button" id="addressSearch"
onclick="location.href='@Url.Action("List", "Search")'">
试试这个:
@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
在你的代码中应该是,
@Html.ActionLink("Search", "List", "Search", new{@class="btn btn-info", @id="addressSearch"})