Asp.net mvc MVC 3 JSON字符串未序列化为控制器操作

Asp.net mvc MVC 3 JSON字符串未序列化为控制器操作,asp.net-mvc,json,asp.net-mvc-3,Asp.net Mvc,Json,Asp.net Mvc 3,目前正在使用MVC3和jQuery$.post函数将ajax请求发送到名为“SearchInitiated”的控制器操作。我有点头疼,因为我不确定我的问题到底在哪里。我相信这是我忽略了的一件小事 当我从AJAX调用调用控制器方法时,我将json对象(字符串化)传递给控制器操作。见下文: 从Chrome请求头文件 接受:/Content Type:application/x-www-form-urlencoded; charset=UTF-8用户代理:Mozilla/5.0(Windows NT

目前正在使用MVC3和jQuery$.post函数将ajax请求发送到名为“SearchInitiated”的控制器操作。我有点头疼,因为我不确定我的问题到底在哪里。我相信这是我忽略了的一件小事

当我从AJAX调用调用控制器方法时,我将json对象(字符串化)传递给控制器操作。见下文:

从Chrome请求头文件

接受:/Content Type:application/x-www-form-urlencoded; charset=UTF-8用户代理:Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.17(KHTML,如壁虎)

Chrome/24.0.1312.52 Safari/537.17

X-request-With:XMLHttpRequest

表单数据视图

连接器ID:1

会话ID:97e2d8b2-be9e-4138-91ed-42ef9c6a2cd6

缔约方: {“Tag”:null,“PartyIdentifier”:“1”,“Address”:null,“Address”:null,“Address”:null,“BusinessName”:“CellPhoneNumber”:null,“City”:null,“Country”:null,“DateOfBirth”:null,“EMailAddress”:null,“employeerAddress”:null,“employeerAddress2”:null,“employeercity”:null,“employeerName”:null,“employeerPhoneumber”:null,“employeerState”:null,“EmployerZip”:null,“Fax”:null,“Firstname”:“TEST”,“Lastname”:“USER”,“MailingAddress”:null,“MailingAddress2”:null,“MailingCity”:null,“MailingState”:null,“MailingZip”:null,“Middlename”:null,“PhoneNumber”:null,“State”:null,“TIN”:“1111111”,“WorkPhoneNumber”:null,“Zip”:null}

javascript 控制器 但是,party对象为null

如果我将代码更改为以下内容,则所有内容都将正确反序列化为强类型对象

 public ActionResult SearchInitiated(int connectorId, string sessionId, string party)
 {
     JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            SearchParty sp =
                   json_serializer.Deserialize<SearchParty>(party);
 }
public ActionResult SearchInitiated(int-connectorId、string-sessionId、string-party)
{
JavaScriptSerializer json_serializer=新JavaScriptSerializer();
搜索方sp=
json_序列化程序。反序列化(参与方);
}

我知道我的json字符串是有效的,因为它在第二个代码段中工作,并且在调用中传递了值。我还可能缺少什么?

我会确保您的模型上有[Serializable]属性。另外,确保您的请求指定了party={myjson}.

您只需在控制器中声明一个类“SearchParty”,即可在控制器中检索强类型对象,而无需序列化

public class SearchParty  
{
 public string party { get; set; }
}

public ActionResult SearchInitiated(SearchParty party)
{
 code here....
}

也请检查此项

可能需要将jQuery.ajax的
传统
属性设置为true才能实现序列化

在文档准备就绪后立即放入下面的代码行,如

$(function(){
 jQuery.ajaxSettings.traditional = true;
});
所以这个问题可能会帮助你们进一步尝试这个

public class SearchParty
{
  public string party { get; set; }

}

  [HttpPost]
  public ActionResult SearchInitiated(SearchParty party)
   {     
       ....
       return View();

  }

我通过修改javascript ajax调用来解决我的错误:

 SearchPost(URL, JSON.stringify( {
            'connectorId': '@Model.ConnectorId',
            'sessionId': '@Model.SessionId',            
            'party' :   currentSelectedParty  
        }))

我需要对ajax调用中发送的整个数据对象进行字符串化,而不仅仅是“party”对象

您可以向我们发送一个通过post发送的示例请求吗?为什么不让框架为您完成工作?将签名更改为:
public ActionResult SearchInitiated(int connectorId,string sessionId,SearchParty party)
而且您不必手动反序列化。如果这不起作用,则说明您有问题,因此请将发送到该操作的数据发布。此外,我还看到您指定了“完整”“类名称在声明中,但不在反序列化中。在这种情况下,还有其他名为
SearchParty
的类吗?我用更多的信息更新了这个问题,谢谢大家的帮助
public class SearchParty
{
  public string party { get; set; }

}

  [HttpPost]
  public ActionResult SearchInitiated(SearchParty party)
   {     
       ....
       return View();

  }
 SearchPost(URL, JSON.stringify( {
            'connectorId': '@Model.ConnectorId',
            'sessionId': '@Model.SessionId',            
            'party' :   currentSelectedParty  
        }))