gpt4 book ai didi

c# - 从 Web 方法返回超过 1 项的最佳方法?

转载 作者:太空狗 更新时间:2023-10-30 00:12:11 25 4
gpt4 key购买 nike

你好,我有这样的情况。

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

由于函数是静态的,我无法从网络方法中访问页面元素(标签和下拉列表)。所以我试图返回我想要的 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]

}

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

最佳答案

创建一个 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....

}

关于c# - 从 Web 方法返回超过 1 项的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com