2016-07-28 115 views
0

所以,我有我的目标我想以JSON格式发送(模型)创建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 

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

回答

0

首先创建这样一个PARAM:

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

1

只是要莫代尔类遵循

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