从JavaScript PageMethods自定义的C#数据传输对象 [英] Custom C# data transfer objects from javascript PageMethods

查看:269
本文介绍了从JavaScript PageMethods自定义的C#数据传输对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建,我想以JSON返回到JavaScript方法自定义对象。该对象被创建为一个类在C#。

I've created a custom object that I'd like to return in JSON to a javascript method. This object was created as a class in C#.

什么是从PageMethod的(如果你喜欢[WebMethod的]),以一个javascript onPageMethodCallback()函数返回此对象的最佳方式是什么?我需要能够通过JavaScript来访问该对象的属性,并根据更新(可能使用jQuery)的DOM。

What's the best way to return this object from a PageMethod ([WebMethod] if you like) to a javascript onPageMethodCallback() function? I need to be able to access the properties of this object via javascript and update the DOM according (possibly using jQuery).

感谢您的StackOverflow! :)

Thank you StackOverflow! :)

推荐答案

在服务器端ASP.NET AJAX将处理序列化对象为您服务。例如:

ASP.NET AJAX on the server side will handle serializing the object for you. For example:

public class Name
{
  public string FirstName;
  public string LastName;
}

[WebMethod]
public Name GetName()
{
  Name name = new Name();

  name.FirstName = "Dave";
  name.LastName = "Ward";

  return name;
}

您就可以直接从jQuery的使用基本上是JD链接到同一方法调用PageMethod的。更具体地讲,这里是后有关调用PageMethods与jQuery

You can then call the PageMethod directly from jQuery using basically the same method that JD linked to. More specifically, here's a post about calling PageMethods with jQuery.

服务器将序列化的返回类型为JSON,你就可以像预期的那样访问名称类的属性。在这个例子中, msg.d.FirstName msg.d.LastName

The server will serialize your return type as JSON and you'll be able to access the properties of the Name class as expected. In this example, msg.d.FirstName and msg.d.LastName.

只是提防.D。 它在3.5 增加了安全功能,和是不是present 2.0。

Just watch out for the .d. It's a security feature added in 3.5, and isn't present in 2.0.

这篇关于从JavaScript PageMethods自定义的C#数据传输对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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