2015-02-12 75 views
10

請我工作的MVC網站,我有一個搜索頁面和索引頁上的另一個搜索表單。我想在索引頁面點擊搜索按鈕時調用相同的搜索頁面控制器。以下是我的按鈕在索引頁上的方式。如何調用控制器從按鈕點擊在asp.net MVC 4

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

列表是我的搜索操作從搜索頁和搜索是控制器的名稱。 %20 /搜索/列表%20%>

顯示錯誤的請求:當我點擊上面的按鈕,它的形式

http://localhost:52050/ <%返回URL。我懷疑它從我的路由,我不知道如何實現這一點,請任何幫助,將不勝感激。

下面是我的路由怎麼

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 
      } 
      ); 
+0

使用@ Html.Actionlink(),而按鈕用於導航目的 – 2015-02-12 06:58:12

回答

21

你混合剃刀ASPX語法,如果您的視圖引擎是剃刀只是這樣做:

<button class="btn btn-info" type="button" id="addressSearch" 
      onclick="location.href='@Url.Action("List", "Search")'"> 
+0

謝謝你的男人。從來沒有,但會有所作爲。非常感謝你 – 2015-02-12 07:12:10

+0

@NuruSalihu你應該在asp.net中學習視圖引擎mvc – 2015-02-12 07:34:40

+0

Yah會這樣做,現在我的Dropdown選擇值已經很困難了。它不會改變。 :( – 2015-02-12 07:45:42

6

試試這個:

@Html.ActionLink("DisplayText", "Action", "Controller", route, attribute) 

在你的代碼應該是,

@Html.ActionLink("Search", "List", "Search", new{@class="btn btn-info", @id="addressSearch"}) 
相關問題