使用 MVC 2 中手动定义的 SelectListItems 中的 SelectList 进行呈现问题
我正在使用 ASP.NET MVC 2 (.NET 3.5),并且需要手动定义选项列表。当我这样做时,我会得到一个下拉菜单,其中每个手动条目都显示为“System.Web.Mvc.SelectListItem”。
我的视图模型将列表定义如下:
public SelectList YesNoList
{
get
{
List<SelectListItem> tmpList = new List<SelectListItem>();
tmpList.Add(new SelectListItem {Text = "", Value = ""});
tmpList.Add(new SelectListItem {Text = "Yes", Value = "1"});
tmpList.Add(new SelectListItem {Text = "No", Value = "0"});
YesNoList = new SelectList(tmpList,"");
}
private set{}
}
在视图中,我使用 Html.DropDownList 引用此列表:
Html.DropDownList("FieldName", viewmodel.YesNoList);
我期望在最终网页上呈现的内容应该是这样的:
<select id="FieldName" name="FieldName">
<option value=""/>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
相反,我得到:
<select id="FieldName" name="FieldName">
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>
我不知所措,因为我无法弄清楚为什么返回类型,因此如果有人可以向我指出视图模型定义有什么问题,或者指出更好的方法,我将不胜感激。我犹豫是否要从 C# 类集合中派生 SelectList,因为 SelectList 将提供一种一致的方式来迭代值并显示文本。
预先感谢,希望有人可以提供帮助。
干杯,
J
I am using ASP.NET MVC 2 (.NET 3.5), and need to manually define what shall be an Options list. When I do so I get a drop down menu, with each of the manual entries reading 'System.Web.Mvc.SelectListItem'.
My view model defines the list as such:
public SelectList YesNoList
{
get
{
List<SelectListItem> tmpList = new List<SelectListItem>();
tmpList.Add(new SelectListItem {Text = "", Value = ""});
tmpList.Add(new SelectListItem {Text = "Yes", Value = "1"});
tmpList.Add(new SelectListItem {Text = "No", Value = "0"});
YesNoList = new SelectList(tmpList,"");
}
private set{}
}
In the view I reference this using the the Html.DropDownList:
Html.DropDownList("FieldName", viewmodel.YesNoList);
What I am expecting to be rendered on the final web page should be like:
<select id="FieldName" name="FieldName">
<option value=""/>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
Instead I get:
<select id="FieldName" name="FieldName">
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>
I am at a loss, as I cannot figure out why the type is being returned so would appreciate it if anybody could point out to me what is wrong with the viewmodel definition, or point out a better way. I was hesitant to derive the SelectList from collections of C# classes as the SelectList would provide a consistant way to iterate through the values and display text.
Thanks in advance, hopefully somebody can help.
Cheers,
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下拉列表也可以处理
List
,只需发送它即可。而
你实际上在制作选择列表时做错了。
应该是:
然后忘记我上面的代码。如果您给它列表以及值和文本“key”,您可以对任何列表执行此操作
A dropdown can handle a
List<SelectListItem>
too, just send that in stead.and
you are actually doing it wrong on making the selectlist.
it should be:
and then forget my above code. you can do this with any List, if you give it the list and the value and text "key"
您可以使用编辑器模板来完成此操作。将其命名为“YesNo”并包含以下代码...
然后在您的模型中为您的属性分配一个“YesNo”的 UIHint。这意味着现在 EditorFor 此属性将为您提供一个很好的是/否列表,该列表将绑定为布尔值。
You could do it by using an editor template. Call it 'YesNo' and include the following code...
Then within your model assign a UIHint of 'YesNo' on your property. Which means that now the EditorFor this property will give you a nice Yes/No list that will bind as a boolean.
试试这个代码:
Try this code: