How to pass Textboxes value to Ajax.ActionLink?

In my ASP.NET MVC application, I want the user to add a value to the text box, and then click my Ajax.ActionLink. I want to do something like this:

Ajax.ActionLink ("Go", "Action", "Controller", new {value = textbox1.value})

Or how else can I return the value of this text box to my action? Jquery

+6
ajax asp.net-mvc actionlink
source share
4 answers

You can trigger an action using AJAX $. get method:

<script type="text/javascript"> $(document).ready(function() { $("#t").change(function() { RunAction(); }); RunAction(); }); function RunAction() { var action = '<%= Url.Action("Action", "Controller") %>'; var data = $("#t").serialize(); $.get(action, data); } </script> <input type="text" id="t" /> 
+2
source share

Many thanks to Alexander! Thanks for putting me on the right track. I have not tried your last code, but I was able to get your previous code. Here is the working code. I'm sure this is all stupid, but maybe someone out there could show me a more elegant solution:

  <script type="text/javascript"> $(document).ready(function() { $("#myVal").change(function() { changeActionURL(); }); changeActionURL(); }); function changeActionURL() { var url = '<%= new UrlHelper(ViewContext.RequestContext).Action("Action", "Controller") %>' + '?dup=' + $("#myVal").val(); $("#u").attr('href', url); } </script> <a id="u" href="" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'HellaYeah' });">Report Dupe</a> </p> <div id="response">not done</div> 

My solution, as you can see, is to simply copy the LINK code instead of trying to use the ASP.NET AJAX helper class.

+2
source share

this is how you extract a value from your text field in jQuery

 var input = $('input[name=txt_MyTextBox]').val() 
+1
source share

replace $ .get (action, data); with $ ("# yourTargetId"). load (action, data); you get ajax, as in the following:

 <script type="text/javascript"> $(document).ready(function() { $("#t").change(function() { RunAction(); }); RunAction(); }); function RunAction() { var action = '<%= Url.Action("Action", "Controller") %>'; var data = $("#t").serialize(); $("#yourTargetId").load(action, data); } </script> <input type="text" id="t" /> 
0
source share

All Articles