1

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 to the controller using post method I always have null values (the controller parameter always have null data). I have seen a lot of different ways to do it but no one works for me.

My view ajax code:

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

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

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

}

My controller:

         [HttpPost]
    public IActionResult MostrarTipusPersona(ProjecteModel pm)
    {
        return PartialView("_Ciutada", new Ciutada());
    }
2
  • Something I can spot: In the type field of your ajax object: type : "post" Also please clarify where you are getting nulls? Is it in the return of the Ajax or the data you are posting? Some more info and I can help you. Commented Aug 2, 2017 at 16:06
  • Hi @Terrance00, I receive the null data in the controller, the parameter ProjecteId is always null. I've modified my post to clarify this, lot of thanks! Commented Aug 3, 2017 at 8:38

2 Answers 2

1

First validate the client side data:

Since no html was provided please log your json:

var request = ...
console.log(request);
$.ajax(...)

Make sure your server side expects the right data

As of your code, the server side expects an object with a string member. So let's give it that. We will create an object representing your json object you are sending to the server:

public class ProjectedModel
{
    public string ProjecteId {get;set;}
}

Now modify your method to expect this model:

public IActionResult MostrarTipusPersona(ProjectedModel model)  

PS: It looks like your client side ajax method is expecting json. I would recommend removing dataType: "json" from your ajax method as the datatype will be partialhtml view. (Just receive it as a string - or expand your question so we can be sure.)

EDIT:

Passing a string value seems to be the issue then, there are two things to try:

(1)Convert the data to string with toString() method

 var request = JSON.stringify(
        {'ProjecteId': ($('#ProjecteId').val()).toString()
    });

(2) Do it inline like this:

var request = JSON.stringify({
    'ProjecteId': "'" + $('#ProjecteId').val() + "'"
});

Off course your other option would be to simply send it an integer.

Sign up to request clarification or add additional context in comments.

1 Comment

First of all, lot of thanks for helping me :) The request have these data: {"ProjecteId":"2"}, I think it's correct. The model ProjecteModel (without [FromBody]) is not null but the property ProjecteId have a null value :( The correct dataType is html, and it works (datatype: "html"), I 've changed it just for try other ways. If I use [FromBody] the ProjecteModel is null..
0

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

Thanks for your help!

2 Comments

I thought you wanted to send json specifically. Anyway, the edit should work now.
Well, the time will come when you want to send json - the above answer can be your guide. Happy coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.