【问题标题】:Create Json Object (trying)创建 Json 对象(尝试)
【发布时间】:2016-07-28 11:53:28
【问题描述】:

所以我有我想以 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

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

【问题讨论】:

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


    【解决方案1】:

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

    您可以尝试使用 POST 代替 GET 吗? (GET 的大小也有限制)

    【讨论】:

      【解决方案2】:

      只需让 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};
      }
      

      【讨论】:

        【解决方案3】:

        首先创建一个类似的参数:

        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变量值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-01
          • 1970-01-01
          • 2011-09-23
          • 2022-06-24
          • 1970-01-01
          • 1970-01-01
          • 2019-01-04
          • 2014-08-09
          相关资源
          最近更新 更多