ASP.Net MVC SelectLIst 和 List的问题

发布于 2024-09-07 20:37:00 字数 592 浏览 2 评论 0原文

我正在扩展一个枚举,并且给定以下代码,selectListItems 是一个通用的 SelectListItems 列表,其中包含我的枚举的所有正确值。

第一个 foreach 循环工作正常。但是,当我创建实际的 SelectList 并传入 selectListItems 时,所有值都会丢失。我怎样才能保持这些价值观完好无损?

foreach (SelectListItem item in selectListItems)
{
    string tex = item.Text;
    string val = item.Value;
    string sel = item.Selected.ToString();
}

SelectList selectList = new SelectList(selectListItems);

foreach (SelectListItem slid in selectList)
{
    string tex = slid.Text;
    string val = slid.Value;
    string sel = slid.Selected.ToString();
}

I'm extending an Enum and, given the following code, selectListItems is a generic List of SelectListItems that has all the correct values for my Enum.

The first foreach loop works fine. However, when I create the actual SelectList and pass in selectListItems, all the values are lost. How can I keep those values intact?

foreach (SelectListItem item in selectListItems)
{
    string tex = item.Text;
    string val = item.Value;
    string sel = item.Selected.ToString();
}

SelectList selectList = new SelectList(selectListItems);

foreach (SelectListItem slid in selectList)
{
    string tex = slid.Text;
    string val = slid.Value;
    string sel = slid.Selected.ToString();
}

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

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

发布评论

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

评论(1

相对绾红妆 2024-09-14 20:37:03

您需要更改构建它的行以告诉它在哪里查找值。在您的情况下,它将是:

SelectList selectList = new SelectList(selectListItems, "Value", "Text");

但这不会保留所选项目。在这种情况下,您需要弄清楚应该选择哪个项目,并通过第四个参数传递它的值。

这是一个示例:

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "Test1", Value = "1", Selected = false });
items.Add(new SelectListItem() { Text = "Test8", Value = "8", Selected = true });
items.Add(new SelectListItem() { Text = "Test3", Value = "3", Selected = false });
items.Add(new SelectListItem() { Text = "Test5", Value = "5", Selected = false });

SelectList sl = new SelectList(items, "Value", "Text", "8");

您可能还想查看

编辑:刚刚看到您的评论,它不起作用,因为它不够智能,无法知道默认情况下查找 TextValue 字段。您可以向它传递一种对象类型,它使您能够通过定义将哪些属性映射到 TextValue 属性来绑定到它。

You need to change the line where you build it to tell it where to look for the values. In your case it would be:

SelectList selectList = new SelectList(selectListItems, "Value", "Text");

This will not carry over the selected item though. In that case you will need figure out which item should be the selected one and pass it's value in via the forth param.

Here is an example:

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "Test1", Value = "1", Selected = false });
items.Add(new SelectListItem() { Text = "Test8", Value = "8", Selected = true });
items.Add(new SelectListItem() { Text = "Test3", Value = "3", Selected = false });
items.Add(new SelectListItem() { Text = "Test5", Value = "5", Selected = false });

SelectList sl = new SelectList(items, "Value", "Text", "8");

You might also want to check out this SO thread that might be helpful.

Edit: Just saw your comment, and it doesn't work because it isn't smart enough to know to look for the Text and Value fields by default. You could pass it an type of objects and it gives you the ability to bind to it by defining which properties to map to the Text and Value properties.

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