家属的DropDownList(级联) [英] Dependant dropdownlist(cascading)

查看:119
本文介绍了家属的DropDownList(级联)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的DropDownList列出的名字。

我试图创建另一个下拉列表contaings所有字母

我试图做到的是:

当一个用户从字母Dropsownlist字母表,第二dropdwonlist将填充所有与选定的字母开始的姓名。

我有这个code。

 `NamesDropDownList.SelectedValue = (NamesDropDownList.DataValueField).Where(NamesDropDownList.SelectedItem.Value).Contains(AlphabetsDropDownList.SelectedItem.Value);`

但它给我的错误:

 错误信息:字符串不包含凡和方法重载包含无效参数的定义。

任何帮助或处理这个问题。

感谢


解决方案

有几件事情错了code的那一条线。让我们先从数据的来源。这不是你的数据的来源:

  NamesDropDownList.DataValueField

这只是一个字符串的属性的DropDownList 。你不能选择的的,你必须记录从选择他们的数据库的(或任何你的伴奏数据)。您还没有提供这方面,所以我会想这是一些标准的LINQ可查询的数据源。比方说,为求或例如,它是这样的:

  dbContext.Names

的是你为了选择数据附加什么是where子句来。所以,现在让我们继续以该条款,看看它是什么样子。对于初学者来说,它的的是这样的:

<$p$p><$c$c>.Where(NamesDropDownList.SelectedItem.Value).Contains(AlphabetsDropDownList.SelectedItem.Value)

。凡()方法不指望一个字符串,它需要一个 Func键&LT; T,BOOL&GT; 为predicate。的的是predicate就是你有你的。载有(),这将在操作字符串并没有对的整个集合的。因此,它可能是这个样子:

  dbContext.Names.Where(N =&GT; n.Name.Contains(AlphabetsDropDownList.SelectedItem.Value))

什么这行code的主要作用是:


  

姓名在数据库表中,选择所有记录中,其中名称列包含给定值。


这会给你过滤后的数据源中的记录,它可以被用来绑定到一个的DropDownList

I have an existing dropdownlist that lists names.

I am trying to create another drop down list that contaings all Alphabets

What i am trying to accomplish is:

When a user selects an alphabet from the Alphabet Dropsownlist, the second dropdwonlist will populate all the names that start with the selected Alphabets.

I had this code.

`NamesDropDownList.SelectedValue = (NamesDropDownList.DataValueField).Where(NamesDropDownList.SelectedItem.Value).Contains(AlphabetsDropDownList.SelectedItem.Value);`

but it is giving me an error:

Error Message: string does not contain a definition of Where, and the method overload contains invalid arguments.

Any Help Or approach to this problem.

thanks

解决方案

There are several things wrong with that one line of code. Let's start with the source of the data. This is not the source of your data:

NamesDropDownList.DataValueField

That's just a string property on a DropDownList. You can't select records from that, you have to select them from the database (or wherever your backing data is). You haven't provided that context, so I'm going to suppose it's some standard LINQ-queryable data source. Let's say, for the sake or example, that it's something like this:

dbContext.Names

That is what you'd attach a "where" clause to in order to select data. So now let's move on to that clause and see what it looks like. For starters, it doesn't look like this:

.Where(NamesDropDownList.SelectedItem.Value).Contains(AlphabetsDropDownList.SelectedItem.Value)

The .Where() method doesn't expect a string, it expects a Func<T, bool> as a predicate. Inside that predicate is where you'd have your .Contains(), which would operate on the string and not on the whole collection. So it might look something like this:

dbContext.Names.Where(n => n.Name.Contains(AlphabetsDropDownList.SelectedItem.Value))

What this line of code essentially does is:

From the Names table in the database, select all records where the Name column contains the given value.

That would give you the filtered set of records from the data source, which could then be used to bind to the next DropDownList.

这篇关于家属的DropDownList(级联)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆