具有动态数据类型的ASP.Net WebMethod的jQuery AJAX POST对象 [英] jQuery AJAX POST object to ASP.Net WebMethod having dynamic datattypes

查看:211
本文介绍了具有动态数据类型的ASP.Net WebMethod的jQuery AJAX POST对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




任何人都知道我在这里做错了什么。我正在尝试使用jQuery AJAX将
POST JSON对象发送到ASP.Net WebMethod。


虽然我在服务器端获取对象但不是我想要的b $ b方式。我希望客户对象是一个简单的对象,所以我可以像普通实例一样访问,例如customer.Name,但我不能,因为它是作为字典对象到达那里。


:为什么JSON在服务器端作为c#动态类型的Dictionary对象?


这是快速观看屏幕播放;

Hi,
Anyone have an idea what I am doing wrong here. I'm trying to POST JSON objects to ASP.Net WebMethod using jQuery AJAX.

Although I'm getting the objects on server side but not the way I wanted. I wanted customer object to be a simple object so I can access like normal instances e.g. customer.Name, but I can't because it's getting there as dictionary object.

: Why JSON is getting on server side as Dictionary object for c# dynamic type?

Here's the quick watch screen-cast;

这是javascript和服务器端代码。

Here's javascript and server side code.

 function SaveCustomer() {
  var funcParams = JSON.stringify({
    customer: {
      Name: "Name of Customer",
      Title: "President"
    },
    address: {
      Street: "Street",
      City: "",
      Zip: ""
    }
  });

//我尝试使用以下json参数,但结果仍然相同。

// I tried with the following json parameters but still same result.

var funcParams = "{\"customer\":" + JSON.stringify({ Name: "Name of
 Customer", Title: "President" }) + ",\"address\":" + 
JSON.stringify({ Street: "Street", City: "", Zip: "" }) + "}";
}

 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8",
 dataType: "json", url: "aspxPage.aspx/SaveCustomer", data: funcParams,
 success: function(){  }, error: function(e){alert("Error occured"+e);} 
 })


[WebMethod(EnableSession = true)]
public static string SaveCustomer(dynamic customer, dynamic address)
{
     if(!string.IsNullOrEmpty(customer.Name) && 
         !string.IsNullOrEmpty(customer.Title)....)
      {
           //app logic
      }
}

推荐答案

我的建议是创建DTO对象以匹配该JSON,然后将您的方法参数更改为Customer和Address类型而不是动态。像这样,例如: http:// encosia。 com / using-complex-types-to-make-calling-services-less-complex /

My suggestion would be to create DTO objects to match that JSON, and then change your method parameters to be of type Customer and Address instead of dynamic. Like this, for example: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

你可以使用 Visual Studio的将JSON粘贴为类可以非常快速/轻松地创建这些类。

You can use Visual Studio's "Paste JSON as Classes" to make creating those classes very quick/easy too.

如果绝对不能使用专用的DTO类某些原因,尝试使用 ExpandoObject 作为参数的类型,而不是 dynamic 。自从我使用它以来已经很长时间了,我不记得它们是否在WebMethods中正确反序列化,但我认为它们确实如此。请理解,与常规DTO POCO相比,动态方法会产生性能损失。

If you absolutely cannot use dedicated DTO classes for some reason, try using ExpandoObject as the type of the parameters instead of dynamic. It's been a very long time since I've worked with that and I don't remember if they deserialize correctly in WebMethods, but I think they did. Just understand that the dynamic approach will incur a performance penalty compared to regular DTO POCOs.

这篇关于具有动态数据类型的ASP.Net WebMethod的jQuery AJAX POST对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆