Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call controller from the button click in asp.net MVC 4

Please i am working on MVC website, i have a Search page and another search form on index page. I want to call the the same search page controller when i click the search button from index page. Below is how my button is on the index page.

    <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 is my search Action from search page and Search is the controller name. When i click the button above, it returns url in the form

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

Showing bad request . I am suspecting its from my Routing , I am not sure how to archieve this, Please any help would be appreciated .

Below is how my Routing is

 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
            }
            );
like image 554
Nuru Salihu Avatar asked Feb 12 '15 06:02

Nuru Salihu


People also ask

How can call POST action method on button click in MVC?

click(function () { //On click of your button var property1 = $('#property1Id'). val(); //Get the values from the page you want to post var property2 = $('#property2Id').


2 Answers

You are mixing razor and aspx syntax,if your view engine is razor just do this:

<button class="btn btn-info" type="button" id="addressSearch"   
          onclick="location.href='@Url.Action("List", "Search")'">
like image 142
Ehsan Sajjad Avatar answered Sep 20 '22 19:09

Ehsan Sajjad


Try this:

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

in your code should be,

@Html.ActionLink("Search", "List", "Search", new{@class="btn btn-info", @id="addressSearch"})
like image 23
Kinjal Gohil Avatar answered Sep 19 '22 19:09

Kinjal Gohil