Best way to return more than 1 item from Web method?
Hi I Have a situation like this.
I have to populate 2 labels and 1 drop-down list in the UI with a call to a Web-method.
As the function is static , I am unable to access the page elements (开发者_JAVA技巧labels and drop-down list ) from within the web-method. So I am trying to return the HTML that I want.
[WebMethod()]
public static object[] GetStatus()
{
//Return text for Label1;
//Return text for Label2;
//Return items to display in ListBox [Number of items can vary]
}
I think object[] may work ..But is it the best way to handle this situation ? Also considering the java script code needed to set the value for these controls (after calling the web-method) what is the best practice in such scenarios ?
create a ViewModel class.
public class StatusViewModel
{
public string Label1 { get; set; }
public string Label2 { get; set; }
public IDictionary<string, string> ListBox { get; set; }
}
[WebMethod()]
public static StatusViewModel GetStatus()
{
// do work
return new StatusViewMode....
}
How about string[]
? Have you tried that?
Create a composite object and return that.
public class combinedObject{
public string Labe11;
public string Label2;
..
}
create custom object witch containing matching properties. then write a custom serialization to serialize and deserialize.
精彩评论