Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to put a partialview when the value of a dropdownlist changes. With my code it works but when I want to send data using post method I always have null values. I have seen a lot of different ways to do it but no one works for me.

What I have tried:

My view ajax code:

<pre>@section scripts {
        <script type="text/javascript">

            $("#ProjecteId").on("change", function () {
                var request = JSON.stringify({
                    'ProjecteId': $('#ProjecteId').val()
                });
                $.ajax({
                    type: 'Post',
                    url: '/Consultas/MostrarTipusPersona',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: request,
                    success: function (data) {

                        console.log('sample', data);
                    },
                    error: function () {
                    }
                });
            });
        </script>
    }

My controller:

[HttpPost]
    public IActionResult MostrarTipusPersona([FromBody]string ProjecteId)
    {
        string i = ProjecteId;
        return PartialView("_Ciutada", new Ciutada());
    }
Posted
Updated 3-Aug-17 3:56am
Comments
Kornfeld Eliyahu Peter 2-Aug-17 8:59am    
Have you debug your JS? What $('#ProjecteId').val() returns?

I've the solution, just put contetType: 'application/x-www-form-urlencoded'.

Thanks for your help!
 
Share this answer
 
Your 'request' variable having null value file catching in controller from ajax request, you may check it using break point. Do it like done below.

<script type="text/javascript">
 
                $.ajax({
                    type: 'Post',
                    url: '/Consultas/MostrarTipusPersona',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: {ProjecteId: $('#ProjecteId').val()},
                    success: function (data) {
 
                        console.log('sample', data);
                    },
                    error: function () {
                    }
                });
            });
        </script>
 
Share this answer
 
Comments
Norbert F.D. 3-Aug-17 5:03am    
I have null values in the controller with your solution :(

Lot of thanks fro trying help me.

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