繁体   English   中英

使用MVC VS2010 AJAX进行级联下拉

[英]Cascading Dropdown with MVC VS2010 AJAX

我正在尝试创建一个Ajax驱动的级联下拉列表。 我发现的示例使用Web服务,但是不附带使用VS2010 Web服务。 相反,我以为我会使用MVC。 我遵循了另一个教程,该教程显示了如何设置MVC服务,但是我的迭代无效。 由于我还没有做过Web服务器或MVC服务,因此我认为这不应该那么难,但是我已经搁浅了。 有人可以指出我做错了什么吗? 我收到此错误“编译器错误消息:CS0524:'位置':接口无法声明类型”

请参阅下面的代码。 谢谢。 里升

[ServiceContract]
public interface ILocations
{
    [OperationContract]
    List<Locations> GetLocations(int ident);

    [DataContract] 
    public class Locations   <<- ERROR *******************************
    {
       int ident = 0;
       string description = string.Empty;

       [DataMember]
       public int Ident
       {
           get { return ident; }
           set { ident = value; }
       }

       [DataMember]
       public string Description
       {
           get { return description; }
           set { description = value; }
       }
    }
}


public class Locations : ILocations
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection();
    string connString = WebConfigurationManager.ConnectionStrings["dbLocations"].ToString();

List<ILocations.Locations>GetLocations(int ident)
{
        cmd.Connection = new SqlConnection(connString);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_Location_Get_Children";

        cmd.Parameters.Add("@pintParentIdent", SqlDbType.Int);
        cmd.Parameters["@pintParentIdent"].Value = ident;

        List<ILocations.Locations> _location = new List<ILocations.Locations>();

        try
        {
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dtLocations = new DataTable();
           da.Fill(dtLocations);

           if (dtLocations.Rows.Count > 0)
           {
                for (int i = 0; i < dtLocations.Rows.Count; i++)
                {
                    ILocations.Locations _locInfo = new ILocations.Locations();
                    _locInfo.Ident = Convert.ToInt32(dtLocations.Rows[i]["ident"]);
                    _locInfo.Description = dtLocations.Rows[i]["description"].ToString();

                   _location.Add(_locInfo);
                }
            }
            return _location;
        }
        catch (Exception ex)
        {
            cmd.Connection.Close();
            throw;
        }
    }
 }


<ajaxToolkit:CascadingDropDown ID="cddLocations" runat="server"
         TargetControlID="ddlLocationDetail"
         Category="description"
         PromptText="Select Location"
         LoadingText="Loading..."
         ServicePath="~/App_Code/ILocations.cs"
         ServiceMethod=""
         ParentControlID="ddlRoomLocation"
         SelectedValue="ident" />
        <asp:DropDownList runat="server" ID="ddlRoomLocation" AppendDataBoundItems="True"
             DataTextField="description" DataValueField="ident"
             width="246px" CssClass="AssetMngnt-smallFont" AutoPostBack="true" 
             OnSelectedIndexChanged="ddlRoomLocation_OnSelectedIndexChanged" >
            <asp:ListItem Value="-1" Selected="True">-- Select a Location --      </asp:ListItem>
            <asp:ListItem Value="-2">-- All Printers --</asp:ListItem>
        </asp:DropDownList>

        <asp:DropDownList Visible="false" runat="server" ID="ddlLocationDetail" AppendDataBoundItems="True"
             DataTextField="description" DataValueField="ident"
             width="246px" CssClass="AssetMngnt-smallFont" AutoPostBack="true" 
             OnSelectedIndexChanged="ddlLocationDetail_OnSelectedIndexChanged" >
            <asp:ListItem Value="-1" Selected="True">-- Select a Detail --</asp:ListItem>
        </asp:DropDownList>

从接口“ ILocations”声明中取出“ Locations”类声明。 那些不能嵌套。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM