开发者

.NET ASMX AJAX returned object

Suppose I have a modal pop open which is populated using ajax calls and javascript.

I have already written classes for my data objects in the backend which I'd like to use, which come from my database but for arguments sake are just dummy classes:

public class Foo {
    public string Property1 { get;set; }
    public string Property2 { get;set; }
}

public class Bar { 
    public int Id { get;set; }
    public int Name { get;set; }
}

I need data from both of these objects already received from the database. I would like to know what the best approach is for returning them back to the client side. I have already come up with a few ideas:

  • Two web methods calls (Seems like an extra XHR request)
  • One call which returns an object array, with index zero being Foo and index one being bar.
  • Create a new structure which encapsulates these two classes

For this particular example, I could work around it but I feel like this is going to come up a lot within what I am coding and I figured I'd ask before coming up with a solution. Anyone have any experience/feedback? If WCF helps me for this particular problem in any way, I'd definitely be willing 开发者_StackOverflow社区to make the switch.

I am using jQuery if that makes a difference.


Please, create a wrapper class that includes the two class instances:

public class FooBar {
    public Foo foo { get; set; }
    public Bar bar { get; set; }
}

This makes sure that, at the client side:

  • You do not have to make assumptions on the location of the object based on its index.
  • The client code is better readable and maintainable. There is little room for misinterpretation.
  • You can have a single return argument for your service operation.

At least, that is my opinion :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜