我如何添加一个项目在ASP.net MVC一个的SelectList [英] How can I add an item to a SelectList in ASP.net MVC

查看:280
本文介绍了我如何添加一个项目在ASP.net MVC一个的SelectList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我期待在以0默认值和文本价值的SelectList的开头插入项目 - 选择一个 -

Basically I am looking to insert an item at the beginning of a SelectList with the default value of 0 and the Text Value of " -- Select One --"

SelectList list = new SelectList(repository.func.ToList());
ListItem li = new ListItem(value, value);
list.items.add(li);

可以这样做?

推荐答案

实在没有必要做这个,除非你坚持为0的值的HtmlHelper DropDownList的扩展允许您设置一个选项标签,显示了在选择一个空值的初始值。只需使用具有选项标签DropDownList的签名之一。

There really isn't a need to do this unless you insist on the value of 0. The HtmlHelper DropDownList extension allows you to set an option label that shows up as the initial value in the select with a null value. Simply use one of the DropDownList signatures that has the option label.

<%= Html.DropDownList( "DropDownValue",
                       (IEnumerable<SelectListItem>)ViewData["Menu"],
                        "-- Select One --" ) %>

这篇关于我如何添加一个项目在ASP.net MVC一个的SelectList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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