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.
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>
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With