Pass textbox value from view to buttons

江枫思渺然 提交于 2019-12-23 15:44:05

问题


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.


回答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




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/19272570/pass-textbox-value-from-view-to-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!