我可以用来填充 DropDownList 的最舒服的数据类型是什么?

发布于 2024-09-15 05:39:35 字数 609 浏览 6 评论 0原文

例如,我希望用户为给定团队创建一个新的粉丝俱乐部。

显然,团队必须加载到 DropDownList 中,以便从预设的选择组中进行选择。团队列表是从数据库返回的。

截至目前,我正在做这样的事情:

//FindAll() returns IQueryable<Team>
var Teams = teamsRepo.FindAll().AsEnumarable();
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "ID";
myDropDownList.DataSource = Teams;
myDropDownList.DataBind();

不幸的是,当以这种方式做事时,我没有强类型属性,因此存在拼写错误 ValueField 或 TextField 的风险。

尝试获取 DropDownList 的选定值时也存在问题。使用以下代码,无论选择哪个团队,所有内容都会以 ID 1 保存。

fansite.IDTeam = myDropDownList.SelectedIndex;

关于如何改进有什么建议吗?

For example, I want a user to create a new fanclub for a given Team.

Obviously the teams have to be loaded into a DropDownList so they are chosen from a pre-set choice group. The list of teams are returned from a database.

As of now I'm doing things like this:

//FindAll() returns IQueryable<Team>
var Teams = teamsRepo.FindAll().AsEnumarable();
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "ID";
myDropDownList.DataSource = Teams;
myDropDownList.DataBind();

Unfortunately when doing things this way I have no strongly typed attributes, so there is a risk of misspelling the ValueField or TextField.

There is also a problem when trying to get the selected value of the DropDownList. Using the following code, everything is saved with ID 1 regardless of what team was chosen.

fansite.IDTeam = myDropDownList.SelectedIndex;

Any suggestions on how to improve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-09-22 05:39:36

从您的问题来看,您似乎担心代码中数据绑定的硬编码字符串。同样的事情也曾经让我发疯。

有一种使用 lambda 方法和表达式树的好方法,它允许您摆脱硬编码字符串并以强类型方式获取属性名称:

您可以使用此类和扩展方法...

public class Nameof<T>
{
    public static string Property<TProp>(Expression<Func<T, TProp>> expression)
    {
        var body = expression.Body as MemberExpression;
        if(body == null)
            throw new ArgumentException("'expression' should be a member expression");
        return body.Member.Name;
    }
}

...并且这将允许您执行以下操作:

//instead of myDropDownList.DataTextField = "Name";
myDropDownList.DataTextField = Nameof<Team>.Property(t => t.Name);
//instead of myDropDownList.DataValueField = "ID";
myDropDownList.DataValueField = Nameof<Team>.Property(t => t.ID);

这就是我喜欢看到的;看不到硬编码的字符串文字! :-)

From your question it seems you're concerned about the hard coded strings for the databinding in your code. The same thing used to drive me nuts too.

There is a good way of using lambda methods and expression trees that will allow you to get rid of the hard coded strings and get the property names in a strongly typed way:

You can use this class and extension method...

public class Nameof<T>
{
    public static string Property<TProp>(Expression<Func<T, TProp>> expression)
    {
        var body = expression.Body as MemberExpression;
        if(body == null)
            throw new ArgumentException("'expression' should be a member expression");
        return body.Member.Name;
    }
}

...and this will allow you to do the following:

//instead of myDropDownList.DataTextField = "Name";
myDropDownList.DataTextField = Nameof<Team>.Property(t => t.Name);
//instead of myDropDownList.DataValueField = "ID";
myDropDownList.DataValueField = Nameof<Team>.Property(t => t.ID);

That's what I like to see; not a hard coded string literal in sight! :-)

执手闯天涯 2024-09-22 05:39:36

这正是 .NET 数据绑定的工作原理,您必须输入属性名称。

您始终可以将转换器添加到组合框项,然后调用 add (ms-doc)

至于获取所选项目的值,请尝试以下操作:

fansite.IDTeam = myDropDownList.SelectedValue;

This is exactly how .NET databinding works, you have to type in the property names.

You could always add a converter to a combobox item and then call add (ms-doc)

As far as getting the value of the selected item try this:

fansite.IDTeam = myDropDownList.SelectedValue;
南汐寒笙箫 2024-09-22 05:39:36

(这是在ASP.Net澄清之前写的,我是从winforms的角度回答的)。

我不确定您的 Teams ID 是什么样的,但如果它们不是连续的、从零开始且没有间隙的,则使用 myDropDownList.SelectedIndex; 会给您错误的结果一条数据。

我认为您最后一行应该阅读:

fansite.IDTeam = myDropDownList.SelectedValue;

在要使用的数据类型上,我通常使用 Dictionary 作为下拉列表,并有一些辅助扩展方法来使填充更好一点。

(This was written before the ASP.Net clarification, I have answered from a winforms persepective).

I'm not sure what your Teams IDs are like, but if they are not sequential, zero based and with no gaps, using myDropDownList.SelectedIndex; will give you the wrong piece of data.

I think you last line should read:

fansite.IDTeam = myDropDownList.SelectedValue;

On the data type to use, I usually use a Dictionary<int, string> for dropdown lists, and have a few helper extension methods to make population a little nicer.

本王不退位尔等都是臣 2024-09-22 05:39:36

您应该考虑使用ObjectDataSourceLinqDataSource 等数据源来数据绑定您的控件。

它将节省您的时间并且不易出错,因为您将通过向导配置设置,而不是在代码隐藏中定义它们。

You should consider using DataSources like ObjectDataSource or LinqDataSource to DataBind your controls.

It will save you time and is less error prone because you will configure your settings through a wizard instead of defining them in your code-behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文