从Web方法返回多个项目的最佳方法?

Ana*_*nth 4 c# asp.net

嗨,我有这样的情况.

我必须通过调用Web方法在UI中填充2个标签和1 个下拉列表.

由于该函数是静态的,我无法从Web方法中访问页面元素(标签和下拉列表).所以我试图返回我想要的HTML.

 [WebMethod()]
    public static object[]  GetStatus()
    {
        //Return text for Label1;

        //Return text for Label2;

        //Return items to display in ListBox  [Number of items can vary]

    }
Run Code Online (Sandbox Code Playgroud)

我认为object []可能有用..但这是处理这种情况的最佳方法吗?还要考虑设置这些控件的值所需的java脚本代码(在调用web方法之后),这种情况下的最佳实践是什么?

Dav*_*ick 6

创建一个ViewModel类.

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

}
Run Code Online (Sandbox Code Playgroud)