ASP.NET Dropdown Count始终返回0

时间:2014-02-27 01:05:33

标签: c# asp.net

在我的asp.net页面中,我使用Ajax方法调用API控制器,并在用户根据其他条件单击下拉框时获取值。这有助于避免页面刷新。

ASP.NET网页代码

<script> 
    $('#<%= ddOtherLocation.ClientID %>').click(function (e) {                
        var postData = {
            deptid: $('#<%= ddLocation.ClientID %>').val()                         
        };

        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: 'path/GetEmployeeDept',
            data: postData,
            dataType: "json",
            success: function (Result) {
                $('#<%= ddOtherLocation.ClientID %>').empty();
                $('#<%= ddOtherAdLocation.ClientID %>').append($("<option value=0>Select</option>"));
                $.each(Result, function (key, value) {
                    $('#<%= ddOtherLocation.ClientID %>').append($("<option></option>").val(value.DeptID).html(value.DeptName));
                });
            },
            error: function (xhr, err) {
                alert("Error occured" + xhr.responseText);
            }
        });                   
        }
    });
</script>
<asp:UpdatePanel ID="upCustAd" runat="server">
    <Triggers>            
        <asp:PostBackTrigger ControlID="btnSave" />
    </Triggers>
    <ContentTemplate>
        <asp:DropDownList ID="ddOtherLocation"
             DataTextField="Text"
             DataValueField="Value"
             runat="server"
             EnableViewState="true"
             CssClass="dropdownheight"
             ClientIDMode="Static">
        </asp:DropDownList>
        ------
        -----
        <asp:Button ID="btnSave" Text="Submit" runat="server" Width="100px" OnClick="CreateSomeThing" />
    </ContentTemplate>
</asp:UpdatePanel>

API控制器代码

public class DeptController : ApiController
{
    public List<DeptInfo> GetEmployeeDept(string deptid)
    {
        .......
        return listofItems;
    }
}

当我提交页面时,下拉列表总是在csharp代码隐藏页面中将项目计数返回为0,并将selecteditem值返回为0。我的方法有什么问题吗?

2 个答案:

答案 0 :(得分:0)

您的网址似乎缺少网页的扩展名...

$.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        **url: 'path/GetEmployeeDept',**
        data: postData,
        dataType: "json",
        success: function (Result) {
            $('#<%= ddOtherLocation.ClientID %>').empty();
            $('#<%= ddOtherAdLocation.ClientID %>').append($("<option value=0>Select</option>"));
            $.each(Result, function (key, value) {
                $('#<%= ddOtherLocation.ClientID %>').append($("<option></option>").val(value.DeptID).html(value.DeptName));
            });
        },
        error: function (xhr, err) {
            alert("Error occured" + xhr.responseText);
        }
    });

应该像url:&#39; path / GetEmployeeDept.aspx&#39; ?

答案 1 :(得分:0)

  1. 尝试用$(document).ready(function(){... your code ...})包装你的js;因为看起来DOM没有准备好,并且没有呈现id ddOtherLocation.ClientID的控件。

  2. 服务器控件中DropDownList的列表项存储在ViewState中,不通过回发传递。在这种情况下,您有0个项目,因为在页面加载/ init / etc期间没有项目存储在viewstate中或者被添加到DropDownList中。