创建Json对象(尝试)

时间:2016-07-28 11:53:29

标签: c# jquery json asp.net-mvc

所以我想要以Json格式(模型)发送我的对象

 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 () {

        });
    }

但是在我的asp.net mvc控制器上看到了

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

关于为什么格式不正确的任何想法?

3 个答案:

答案 0 :(得分:2)

GET方法将一些字符转换为url编码字符。 (见:http://www.w3schools.com/tags/ref_urlencode.asp

您可以尝试使用POST而不是GET吗? (GET的大小也有限)

答案 1 :(得分:1)

让As Modal类跟随

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};
}

答案 2 :(得分:0)

首先创建一个像:

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

然后将其传递给控制器​​,如: -

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

然后在控制器中放入debugger并检查model变量值。