D-W
D-W

Reputation: 5341

Create Json Object (trying)

So I have my object I want to send in Json format (model)

 function SerachClient() {

        var tempfirstname = $("#firstname").val();
        var templastname = $("#lastname").val();
        var tempmobile = $("#mobile").val();
        var tempaccountId = $("#AccountId").val();
        var tempPin = "1234";
        var model = { LastName: templastname, FirstName: tempfirstname, Mobile: tempmobile, AccountId: tempaccountId, Pin: tempPin }
        $.ajax({
            url: "/Home/SearchClient/",
            type: 'GET',
            data: { model: JSON.stringify(model) },
            cache: false,
            crossDomain: true,
            async: true,
            dataType: 'json',
            success: function (data) {

            },
            error: function (event) {

            },
            headers: {
                'Access-Control-Allow-Origin': '*'
            },
        }).done(function () {

        });
    }

however on my asp.net mvc controller it sees

public JsonResult SearchClient(string model)
{
}
model=%7B%22LastName%22%3A%22Smith%22%2C%22FirstName%22%3A%22John%22%2C%22Mobile%22%3A%2278121212166%22%2C%22AccountId%22%3A%224e82dbfe-2b7f-472c-b66c-0707b1d66ba2%22%2C%22Pin%22%3A%221234%22%7D&_=1469706173642

Any ideas on why its not formatting correctly?

Upvotes: 0

Views: 80

Answers (3)

Ranjith.V
Ranjith.V

Reputation: 326

Just make As Modal class follows

public JsonResult SearchClient(modalclass model)
   {
   string FirstName=model.FirstName;
   string lastname=model.Lastname;
    }


public class modalclass 
{

public string FirstName{get;set};
public string LastName{get;set};
public int Mobile {get;set};
}

Upvotes: 1

Ricardo van Laarhoven
Ricardo van Laarhoven

Reputation: 788

The GET method converts some characters to url encoded characters. (see: http://www.w3schools.com/tags/ref_urlencode.asp)

Could you try using POST in stead of GET? (GET is also limited in its size)

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

First create a param like:

var param = JSON.stringify({
     model = { LastName: templastname, FirstName: tempfirstname, Mobile: tempmobile, AccountId: tempaccountId, Pin: tempPin }
});

and then pass it on to the controller like:-

$.ajax({
     url: "/Home/SearchClient/",
     type: 'GET',
     data: param,

and then put a debugger in the controller and check the model variable value.

Upvotes: 0

Related Questions