0
我正在嘗試創建一個由Ajax驅動的級聯下拉列表。我發現的示例使用Web服務,但是使用VS2010 Web服務不附帶。相反,我認爲我會使用MVC。我已經學習了另一個教程,它展示瞭如何設置MVC服務,但是我的迭代不起作用。因爲我還沒有做過Web服務器或MVC服務,所以我認爲它不應該那麼艱難,但我擱淺了。有人能指出我做錯了什麼嗎?我得到這個錯誤「編譯器錯誤消息:CS0524:'位置':接口不能聲明類型」與MVC VS2010級聯下拉列表AJAX
請參閱我的代碼波紋管。謝謝。 Risho
[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>
您的代碼示例使用DropDownList控件,它是一個Web窗體控件。 ASP.NET MVC中沒有這樣的控件。 – Shyju
這是正確的,我正在建立一個網站。在VS2010中引人注意的是,web服務已經被MVC服務取代,以便與Ajax級聯下拉菜單一起工作。 – Risho