问题与ASP.Net MVC选择列表和List< SelectListItems> [英] Problem with ASP.Net MVC SelectLIst and List<SelectListItems>

查看:624
本文介绍了问题与ASP.Net MVC选择列表和List< SelectListItems>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我伸出一个枚举,鉴于以下code, selectListItems 是SelectListItems的泛型列表具有所有正确的价值观为我的枚举。

第一foreach循环工作正常。然而,当我创建实际的SelectList,并通过在 selectListItems ,所有的值都将丢失。我怎样才能保持这些值不变?

 的foreach(在selectListItems SelectListItem项)
{
    字符串TEX = item.Text;
    字符串VAL = item.Value;
    字符串SEL = item.Selected.ToString();
}的SelectList选择列表=新的SelectList(selectListItems);的foreach(SelectListItem在选择列表滑动)
{
    字符串TEX = slid.Text;
    字符串VAL = slid.Value;
    字符串SEL = slid.Selected.ToString();
}


解决方案

您需要改变你的地方建立它告诉它到哪里寻找的值就行了。在你的情况将是:

 的SelectList选择列表=新的SelectList(selectListItems,值,文本);

这不会对选定的项目,虽然结转。在这种情况下,你需要找出哪些项目应该被选中的一个,通过它在通过第四参数值。

下面是一个例子:

 列表< SelectListItem>项目=新的List< SelectListItem>();
items.Add(新SelectListItem(){文字=测试1,值=1,选择= FALSE});
items.Add(新SelectListItem(){文本=Test8,值=8,选定=真});
items.Add(新SelectListItem(){文字=Test3的VALUE =3,选择= FALSE});
items.Add(新SelectListItem(){文字=TEST5,值=5,选择= FALSE});SL的SelectList =新的SelectList(项目,值,文本,8);

您可能也想看看<一个href=\"http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-to-work\">this SO线程可能会有所帮助。

编辑:刚刚看到你的评论,这是行不通的,因为它不是足够聪明,知道要查找文本默认值字段。你可以通过它的类型的对象和它给你绑定到它的能力,通过定义映射的属性到文本属性。

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();
}

解决方案

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.

这篇关于问题与ASP.Net MVC选择列表和List&LT; SelectListItems&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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