Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing textbox value to controller on button click?

I need to pass textbox data to controller action on a button click. Here is my code:

<input id="txt" type="text">
<button onclick="@Url.Action("MyAction", "Mycontroller", new {currencyCode=ViewBag .currencyCode,endDate=Model.StartDate, value entered in txt above})" > 

I cant use form here. Can you please suggest me how I can access/ pass this value to action ?

Thanks for your help and guiding me.

like image 405
Toubi Avatar asked Aug 03 '26 23:08

Toubi


2 Answers

The code below sends the text-box value to the controller's action.

<input id="txt" type="text">
<button id="button">Click Me</button>

@section Scripts {
    <script type="text/javascript">
        $("#button").click(function () {
            var txtVal = $("#txt").val();
            window.location = "@Url.Action("TheAction","TheController")" + 
                              "/" + txtVal;
        });
    </script>
}
like image 196
Abbas Amiri Avatar answered Aug 06 '26 12:08

Abbas Amiri


You can use something like

<div>
  <input type='text' name='UnboundTextBoxName' />
</div>

MVC will automatically take the value of UnboundTextBoxName and insert that value into the parameter of the same name.

like image 26
Hope Avatar answered Aug 06 '26 12:08

Hope



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!