How to send a value of an input that is assigned by jquery to the controller using Url.Action in MVC C #

0

I assign a value to input by jquery in document ready

 $(document).ready(function () {
      $("#input_1").val(“valor de prueba”);
 });

In the body, there is the input and attempt to send its value by url.action

<body>
<input id=" id=”input_1" runat="server" type="text" value="" />
<a href="<%:Url.Action("Enviar","Test", new { format = "xls",dato_input = input_1.Value})%>">
                <img src="<%:Url.Content("~/Images/iconos/export_btn.png")%>" /></a>
</body>

And in the controller, I receive the data:

public void Enviar (string format, String dato_input)
    {
 }

But when I get to the controller, I realize that dato_input comes null , so on screen, the input has value.

I would like to know how to send data from a input as a parameter to a controller method, I do not know why it does not work the way I do it

    
asked by Danilo 15.09.2016 в 23:27
source

2 answers

1

You are mixing two different paradigms. On the one hand you have a control of ASP.NET WebForms <input id=" id=”input_1" runat="server" type="text" value="" /> , and on the other you want the value of that control to be sent via ActionLink ( tag to ).

Your tag a, whose url you build in the following way, is incorrect:

<%:Url.Action("Enviar","Test", new { format = "xls",dato_input = input_1.Value})%>

Because unlike WebForms, ASP.NET MVC does not maintain an event paradigm that lets you know when your input_1 control changes and has a value. That's why if you check the generated HTML, data_input has no value.

An MVC application must be built under the Request-Response paradigm ( Request-Response Paradigm ), so maybe you want to refactor your code so it looks like this:

<body>
<form id="enviarForm" action="<%:Url.Action("Enviar","Test", new { format = "xls"})%>">
  <input name="dato_input" type="text" value="" />

  <a href="#" onclick="javascript:document.getElementById('enviarForm').submit()">
     <img src="<%:Url.Content("~/Images/iconos/export_btn.png")%>" />
  </a>
</form>
</body>

In this case we build the necessary HTML so that the data is sent to our backend (controller), in this case the most appropriate form would be a form. And to keep your tag structure as a send button, we use a bit of javascript. If the use of the label can be changed by a button to achieve the same effect, please replace it with:

  <button type="submit">
     <img src="<%:Url.Content("~/Images/iconos/export_btn.png")%>" />
  </button>
    
answered by 16.09.2016 в 05:53
0

Based on what I'm seeing, I recommend you obtain the variable in your controller by:

Request["input_1"];

If you start with ASP.net MVC I recommend you see the workshop I did a couple of months ago: link Also check the courses that are on the Microsoft Virtual Academy

    
answered by 16.09.2016 в 01:09