Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to return more than 1 item from Web method?

Tags:

c#

asp.net

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 (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 ?

like image 279
Ananth Avatar asked Dec 17 '22 11:12

Ananth


2 Answers

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....

}
like image 179
David Wick Avatar answered Dec 28 '22 23:12

David Wick


How about string[]? Have you tried that?

like image 35
John Saunders Avatar answered Dec 28 '22 23:12

John Saunders