Click here to Skip to main content
15,884,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all.

I cannot pass the values from the textboxes onm a form to the controller.


view code
<script type="text/javascript">
        $(document).ready(function () {
            $("#btnSubmit").click(function () {
              
                var presc1 = $('#p1').val(),
                    aviad1 = $('#a1').val(),
                   

                $.ajax({
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                   
                    url: "comparar",
                    
                    data: "{'presc1':'" + presc1 + "', 'aviad1':'" + aviad1 + "'}",
 });
            });
        });
</script>


<div Class="container">
   
        @Html.BeginForm("comparar", "prescavi/comparar", FormMethod.Post)
        <div Class="col-md-12 col-xs-12">
            <div Class="col-md-6 col-xs-6">
                <div Class="col-md-4 col-xs-4">

                    <Label for="prescrito1"></Label>
                </div>

                <div Class="col-md-4 col-xs-4">
                    @Html.TextBox("p1")



                </div>
            </div>

            <div Class="col-md-6 col-xs-6">
                <div Class="col-md-4 col-xs-4">
                    <Label for="aviado1"></Label>
                </div>

                <div Class="col-md-4 col-xs-4">
                    @Html.TextBox("a1")
                </div>
            </div>
        </div>

<br><br>

        <div Class="col-md-12 col-xs-12">
            <div Class="col-md-6 col-xs-6">
                <div Class="col-md-4 col-xs-4">

                    <Label for="nada1a"></Label>
                </div>

                <div Class="col-md-4 col-xs-4">
                    <Label for="nada1b"></Label>
                </div>
            </div>

            <div Class="col-md-6 col-xs-6">
                <div Class="col-md-4 col-xs-4">
                    <Label for="nada2a"></Label>
                </div>

                <div Class="col-md-4 col-xs-4">
                    <input type="submit" name="btnSubmit" value="Send" />
                  
                </div>
            </div>
        </div>
</div>




controller code
Public Function Comparar(ByVal presc1 As String, ByVal aviam1 As String)
           
            Return "you entered " & presc1 & " and " & aviam1 & ""
end function



how to pass the data?
thanks in advance
Posted
Updated 30-Sep-15 7:57am
v4
Comments
Zoltán Zörgő 30-Sep-15 14:05pm    
Have you tried to sniff the http traffic?
Richard Deeming 30-Sep-15 14:13pm    
Your form is set to post to "prescavi/comparar", but your AJAX request is posting to "comparar". Which is the correct relative URL?
Nosfa 30-Sep-15 14:52pm    
comparar is a function inside prescavi controller.

it starts the function but only returns
"you entered and"- meaning the variables are empty
Richard Deeming 30-Sep-15 14:59pm    
Try passing a javascript object, instead of a string:
data: { 'presc1': presc1, 'aviad1': aviad1 }
Nosfa 30-Sep-15 15:50pm    
still the same

Hi,

data: "{'presc1':'" + presc1 + "', 'aviad1':'" + aviad1 + "'}",


Please Remove '' quotes

try this one its work out

data : JSON.stringify({ presc1: presc1 , aviad1: aviad1 }),


and one more thing below code your missing semicollon

C#
var presc1 = $('#p1').val(),
                    aviad1 = $('#a1').val(),

Modify that
C#
var presc1 = $('#p1').val(),
                    aviad1 = $('#a1').val();






Thanks
Bharat peggem
 
Share this answer
 
v3
Comments
Nosfa 2-Oct-15 11:15am    
no. still the same.

(the semicolon issue happened because I truncated other variables. after the last on i do have the semicolon)


I think the error is on the controller side.
Must I put httppost?
ActionResult?

thanks
Nosfa 2-Oct-15 11:59am    
ok.
what happens is that the script is not running!
The form method/action is calling the controller, therefor no data is passed.
if I take those off. nothing happens, the form shows blanks and the address bar changes to http://localhost:16844/input/index?p1=28258&a1=52445&p2=&a2=&p3=&a3=&p4=&a4=&btnSubmit=comparar


#btnsubmit should work.
XML
I think the error is on the controller side.
Must I put  <HttpPost>?
ActionResult?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900