【问题标题】:Bind Drop down Using Web Method (how to call web services in C#)使用 Web 方法绑定下拉列表(如何在 C# 中调用 Web 服务)
【发布时间】:2021-01-02 21:58:16
【问题描述】:

我想绑定多个下拉列表,并且所有下拉列表相互依赖。 使用 Web 方法(服务 URL)。 服务返回类型是ExtensionDataObject,我想将此返回类型存储到列表中是否可能我不确定

但是我怎样才能将这个对象绑定到列表中。

ASPX

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var params = { UserId: 'approver01', WorkflowTypeCode: 4 };
                $.ajax({
                    type: "post",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({ "objectData": params }),
                    url: 'IP/Service.svc/getdata/GetCountriesName',
                    dataType: "json",
                    success: BindCountry,
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(xhr.responseText);
                        alert(thrownError);
                    }
                });

                function BindCountry(response) {
                    $.each(response.d, function (key, value) {
                        appendString = "<option value='" + key + "'>" + value + "</option>";
                        $("#ddlCountry").append(appendString);
                    });
                }
                function ajaxError(response) {
                    alert(response.status + ' ' + response.statusText);
                }
            });
        </script>


<select id="ddlCountry" runat="server">
     <option value="0">--Select Country--</option>
</select>
 <select id="ddlCompany" runat="server">
      <option value="0">--Select Company--</option>
 </select>
 <select id="ddlDivision" runat="server">
      <option value="0">--Select Devision--</option>
 </select>
<select id="ddlLocation">
       <option value="0">--Select Location</option>
</select>

.CS 代码 这里我的方法是:为服务引用创建对象,然后添加到列表和返回列表并通过 aspx 调用使用 j 查询 URL 但无法执行相同操作。

 [WebMethod]
 public static List<CountryList> GetCountriesName(string UserID)
 { //Creating object for service reference.
   AlmaraiMasterDataService.MasterDataServiceClient oClient = new AlmaraiMasterDataService.MasterDataServiceClient(); 
   List<CountryList> lst = new List<CountryList>();
   lst.Add(oClient.GetActiveCountries); //Error not able to add In List
   return lst;                                                               
 }
 public class CountryList
   {
    public int CountryId { get; set; }
    public string CountryName { get; set; }
   }

【问题讨论】:

  • 请在后端提供Mymethod的详细信息?
  • @SelimYildiz 我再次更新我的问题

标签: jquery asp.net html-select cascadingdropdown


【解决方案1】:

@nitish:根据我的观察,mymethod 是 jquery url 中的方法 url: 'IP/Service.svc/getdata/Mymethod', 但实际的 webmethod 名称是 GetCountriesName()。我建议也尝试在 jquery url 中更改它。会有所帮助。

【讨论】:

  • 只是为了安全区域我修改了这些东西
  • 我无法在我的代码中提到的列表项中添加国家,您可以查看我的更新代码
  • var params = { UserId: 'approver01', WorkflowTypeCode: 4 };您从 jquery ajax 调用中传递了 2 个参数,但您的 webmethod 没有参数。请检查并告诉我。bcoz 我尝试过并且它现在可以工作了。像这样的数据: JSON.stringify({ UserID: 'approver01', WorkflowTypeCode: 4 } ) 和 [WebMethod] public static List GetCountriesName(string UserID, int WorkflowTypeCode)
  • 我在我的方法中添加了 2 个参数,但我无法将此对象引用添加到列表项请帮助我
  • oClient.GetActiveCountries() 应该返回一个 List 并且你直接将它分配给 List lst;或者如果 oClient.GetActiveCountries() 返回单个项目,你可以使用循环。如果可能的话发布你的 oClient.GetActiveCountries() 代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2018-12-08
相关资源
最近更新 更多