WCF 服务将数据填充到 ASP.net 下拉列表(学习 WCF 服务)

Posted

技术标签:

【中文标题】WCF 服务将数据填充到 ASP.net 下拉列表(学习 WCF 服务)【英文标题】:WCF Service populate data to ASP.net dropdownlist (Learning WCF Service) 【发布时间】:2017-03-10 14:43:24 【问题描述】:

我正在尝试学习 WCF 服务以最终与我们的一个网站合并。

我在我的数据库中创建了一个名为 [TestTable] 的测试表 我已经能够从 Visual Studio 中创建的测试站点成功地将数据写入表中。

我现在正尝试从数据库中填充下拉列表。在这种情况下,是所有状态的列表。

我似乎遗漏了一些东西。我可以调用该服务,它将显示 WCF 测试客户端中的所有状态;但是,我似乎无法弄清楚如何将它返回的列表附加到 ASP.net 下拉列表中。

这是我的 IService1.cs

[ServiceContract]
public interface IService1


    [OperationContract]
    //string GetData(int value);
    string InsertApplicantDetails(ApplicantDetails appInfo);

    [OperationContract]
    List<US_States> GetAllStates();

    //CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here


这是我的 GetAllStates 的 Service1.svc.cs

public List<US_States> GetAllStates()
    
        using (JobSeekersEntities db = new JobSeekersEntities())
        
            var q = (from us_states in db.US_States
                     orderby us_states.abbr ascending
                     select us_states);

            List<US_States> list_States = q.ToList<US_States>();

            return list_States.ToList();

        
    

在测试网站中,我创建了名为“dropdownStates”的下拉列表。 我已经添加了 ServiceReference 并且它正在工作,因为我可以将值提交到数据库。 我还为下拉列表创建了一个 Load() 事件。

这是我的测试页面。

public partial class TestSubmission : System.Web.UI.Page

ServiceReference1.Service1Client objCon = new ServiceReference1.Service1Client();

protected void Page_Load(object sender, EventArgs e)




protected void dropdownStates_Load(object sender, EventArgs e)

    //ServiceReference1.Service1Client myCon = new ServiceReference1.Service1Client();
    dropdownStates.Items = ????


任何帮助将不胜感激。感谢您的宝贵时间。

【问题讨论】:

【参考方案1】:

首先确保您已定义正确的 DataContract 和 Service Contract。 在以下示例中,我定义了具有 2 个属性的美国各州。 注意 [OperationContract] 和 [DataContract] 声明!

namespace WcfService1

    [ServiceContract]
    public interface IService1
    

        [OperationContract]
        List<US_States> GetAllStates();

    


    [DataContract]
    public class US_States
    
        [DataMember]
        public int StateId  get; set; 

        [DataMember]
        public string StateName  get; set; 
    

接下来将 WebService 引用添加到您的项目并进行如下调用: (将 StateName 绑定到 Text,StateId 绑定到 Value)

    protected void dropdownStates_Load(object sender, EventArgs e)
    
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        dropdownStates.Items.Clear();
        dropdownStates.DataSource = client.GetAllStates();
        dropdownStates.DataTextField = "StateName";
        dropdownStates.DataValueField = "StateId";
        dropdownStates.DataBind();
        client.Close();
    

最后是服务:

namespace WcfService1

    public class Service1 : IService1
    
        public List<US_States> GetAllStates()
        
            List<US_States> result = new List<US_States>();
            result.Add(new US_States()  StateId = 1, StateName = "New York" );
            result.Add(new US_States()  StateId = 2, StateName = "Washington" );
            result.Add(new US_States()  StateId = 3, StateName = "Indiana" );
            return result;
        
    

【讨论】:

感谢两位的帮助。这让我启动并运行。我很感激。【参考方案2】:
using(ServiceReference1.Service1Client client = new ServiceReference1.Service1Client())

        dropdownStates.Items.Clear();
        dropdownStates.DataSource = client.GetAllStates();
        dropdownStates.DataTextField = "Name";
        dropdownStates.DataValueField = "Id";
        dropdownStates.DataBind();

您只需将 Dropdown DataSource 设置为列表。根据您想要显示为项目的内容以及它应该具有的值设置文本和数据值字段,最后调用 DataBind。

【讨论】:

以上是关于WCF 服务将数据填充到 ASP.net 下拉列表(学习 WCF 服务)的主要内容,如果未能解决你的问题,请参考以下文章

如何从现有 SQL DB ASP.Net MVC 4 在下拉列表中填充数据

如何使用 ajax 在 Asp.net 中填充下拉列表

如何通过 ajax 使用数据库数据填充 ASP.NET MVC 4 下拉列表

使用 ajax 和 webmethod 在 asp.net 中使用 web 方法和 ajax 级联下拉列表绑定下拉数据

如何验证从 asp.net 中的 SQL 填充的下拉列表(UnobtrusiveValidationMode)错误

根据上一个列表中选择的值填充下拉列表(asp.net mvc3)