3

View:

@using (@Html.BeginForm("Show", "test", FormMethod.Post))
{

    <fieldset>

        <table >
            <tr>
                <td>
                    @Html.LabelFor(model=> model.Show)
                </td>

                <td>
                 @Html.TextBox("txtvalue", null)

                </td>
            </tr>


        </table>


             <input type="button" value="Show" onclick = "@("location.href='"+ @Url.Action("Show", "test")+"'" )" />

           <input type="button" value="Go" onclick ="@("location.href='"+ @Url.Action("Go", "test")+"'" )"/>




    </fieldset>
}

and two action methods in the controller,

public ActionResult Show(string txtvalue)
{
    ...
} 

public ActionResult Go(string txtvalue)
{
    ...
} 

based on which button been click , it should go to the corresponding action method and pass the value of the textbox.

Can anyone suggest me the way to do it. I wrapping my head around couldn't figure out.

software
  • 728
  • 6
  • 18
  • 39

3 Answers3

1

Try this,

<input type="button" value="Show" onclick = "location.href='@Url.Action("Show", "Foo")'"/>
<input type="button" value="Go" onclick = "location.href='@Url.Action("Go", "Foo")'"/>

UPDATE:

As type="button" does not submit the values in the form, its not directly possible to do what you have asked, the better idea is to Identify the Button that has been clicked in the controller method as show in this link

AthibaN
  • 2,087
  • 1
  • 15
  • 22
  • the Go button action method in the controller is not getting value from the textbox from the same view – software Oct 09 '13 at 13:24
  • Not sure, but try using this overload of the `Html.BeginForm'` , `@using (Html.BeginForm())` instead of `@using (@Html.BeginForm("Show", "test", FormMethod.Post))` – AthibaN Oct 09 '13 at 13:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38892/discussion-between-software-and-athiban) – software Oct 09 '13 at 13:50
0

Try something like:

<input type="button" value="Show" onclick="location.href='<%:Url.Action("Show", "ControllerName")%>'"/>


<input type="button" value="Go" onclick="location.href='<%:Url.Action("Go", "ControllerName")%>'"/>

If you are posting more form data you can use Ajax, see Making a Simple Ajax call to controller in asp.net mvc

Community
  • 1
  • 1
tomsullivan1989
  • 2,760
  • 14
  • 21
0

Try this

<input type="button" value="Show" onclick="location.href='<%: Url.Action("Show", "Controller") %>'" />

<input type="button" value="Go" onclick="location.href='<%: Url.Action("Go", "Controller") %>'" />
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71