简体   繁体   中英

Pass textbox value from view to buttons

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.

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

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

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") %>'" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM