LINQ查询下拉列表数据源

时间:2013-09-07 16:25:39

标签: c# asp.net linq entity-framework

我没有任何线索为什么在更改ddlMedicalName的值时未填充ddlCity。我只是尝试构建函数“ddlMedicalName_SelectedIndexChanged”,这样如果有人从下拉列表ddlMedicalName中选择任何MedicalName,则应根据ddlMedicalName的selectedvalue填充另一个下拉列表“ddlCity”数据源。

代码背后:

  protected void ddlMedicalName_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        //string entityString = ConfigurationManager.ConnectionStrings["medicaldb2Entities"].ConnectionString;
        //EntityDataSource eds = new EntityDataSource();
        using (Entity.medicaldbEntities context = new Entity.medicaldbEntities())
        {
            string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

            var orders = from order in context.Cities
                         where order.CityID == slct
                         select new { order.CityID, order.CityName };

            var lstSrc = orders.ToList();

            ddlCity.DataSource = lstSrc;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";

            ddlCity.DataBind();
        }

    }

ASPX:

<tr id="SearchDropDown">

 <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=medicaldbEntities"  
    DefaultContainerName="medicaldbEntities" EnableFlattening="False" 
    EntitySetName="Medicals" Select="it.[medicalName],it.[CityFK]">
 </asp:EntityDataSource>

<td>
<asp:DropDownList ID="ddlMedicalName" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlMedicalName_OnSelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName"
    DataValueField="CityFK">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlRegion_SelectedIndexChanged"
    DataSourceID="EntityDataSource1" DataTextField="MedicalName" 
    DataValueField="MedicalName">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="false"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
     DataTextField="CityID" 
    DataValueField="CityID">
</asp:DropDownList>
</td>
<td>
<asp:Button ID="SearchButton" runat="server" Text="Suche" onclick="SearchButton_Click" />
</td>
</tr>

那就是我得到的错误:

enter image description here

如果我将其更改为:

List<string> lstSrc = orders.ToList();

它不会抛出任何错误但它也不起作用(ddlCity未填充)。

3 个答案:

答案 0 :(得分:3)

您准备query但从未实际使用过它。

var orders = from order in context.Cities
             where order.CityID == slct
             select order.CityName;

List<Entity.City> lstSrc = orders.ToList();

修改

另一件事是,您必须从查询中获取更多CityName

var orders = from order in context.Cities
             where order.CityID == slct
             select new { order.CityID, order.CityName };

它将返回具有两个属性的匿名类型:CityIDCityName。但由于它是匿名类型,因此在声明var变量时必须使用List<T>

var lstSrc = orders.ToList();

或直接将其分配给DataSource属性:

ddlCity.DataSource = orders.ToList();

答案 1 :(得分:1)

string selected = ddlMedicalName.SelectedItem.Value;
            int slct = Convert.ToInt32(selected);

            var orders = from order in context.Cities
                         where order.CityID == slct
                         select order.CityName;

            ddlCity.DataSource = orders.ToList();
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();

检查Autopostback =&#34; true&#34;

<asp:Dropdownlist id="ddlMedicalName" runat="Server" Autopostback="true" ></Dropdownlist>

答案 2 :(得分:0)

我的问题非常具体;在master.site的主要表单标签上有一个警告。表单的结束标记已经存在,但由于某些不允许的控件或标记,它没有被检测到并且没有被渲染,这导致了奇怪的下拉列表问题的原因。然后我编辑了我的html和主要表单标签并摆脱了#34; Form1没有结束标签&#34;警告,一切都还好。