我在控制器中创建一个selectList,以在视图中显示.
我正试图在飞行中创建它,有点像......这样......
myViewData.PageOptionsDropDown =
new SelectList(new [] {"10", "15", "25", "50", "100", "1000"}, "15");
Run Code Online (Sandbox Code Playgroud)
它编译,但输出不好......
<select id="PageOptionsDropDown" name="PageOptionsDropDown">
<option>10</option>
<option>15</option>
<option>25</option>
<option>50</option>
<option>100</option>
<option>1000</option>
</select>
Run Code Online (Sandbox Code Playgroud)
注意没有选择项目?
我怎样才能解决这个问题??
基本上我想在SelectList的开头插入一个项目,默认值为0,文本值为" - 选择一个 - "
就像是
SelectList list = new SelectList(repository.func.ToList());
ListItem li = new ListItem(value, value);
list.items.add(li);
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
在Stack Overflow上有一些关于这个的帖子,但没有一个答案似乎解决了我目前的情况.
我有一个页面,其中包含一个表格,每行有许多文本字段和一个下拉列表.所有下拉列表都需要使用相同的SelectList数据,所以我按如下方式设置:
调节器
ViewData["Submarkets"] = new SelectList(submarketRep.AllOrdered(), "id", "name");
Run Code Online (Sandbox Code Playgroud)
视图
<%= Html.DropDownList("submarket_0", (SelectList)ViewData["Submarkets"], "(none)") %>
Run Code Online (Sandbox Code Playgroud)
我在很多地方使用了这个设置,但由于某种原因,在这个特定的视图中,我得到了错误:
没有类型为"IEnumerable"的ViewData项具有键"submarket_0".
我正在创建一个脚手架 -
rails g scaffold Contact email:string email_provider:string
Run Code Online (Sandbox Code Playgroud)
但我希望电子邮件提供商是一个下拉列表(使用gmail/yahoo/msn作为选项),而不是文本字段.我怎样才能做到这一点 ?
ruby-on-rails input selectlist ruby-on-rails-3 drop-down-menu
无论如何在ViewModel上预先填充了具有数据属性的SelectList吗?
我想要做
@Html.DropdownListFor(m=> m.CityId, Model.Cities);
Run Code Online (Sandbox Code Playgroud)
所以它生成如下代码:
<select id="City" class="location_city_input" name="City">
<option data-geo-lat="-32.522779" data-geo-lng="-55.765835" data-geo-zoom="6" />
<option data-geo-lat="-34.883611" data-geo-lng="-56.181944" data-geo-zoom="13" data-geo-name="Montevideo" data-child=".state1" value="1">Montevideo</option>
<option data-geo-lat="-34.816667" data-geo-lng="-55.95" data-geo-zoom="13" data-geo-name="Canelones, Ciudad de la Costa" data-child=".state41" value="41">Ciudad de la Costa</option>
</select>
Run Code Online (Sandbox Code Playgroud) asp.net-mvc html5 html-helper selectlist custom-data-attribute
堆栈上的菜鸟溢出来了.我正在开发一个具有转移工作职能的网页.这允许用户选中复选框以将作业发送回办公室,或从所有可用列表中选择技术人员.我的问题是如何设置复选框,以便在页面加载时默认选中它并相应地禁用选择列表.这是我目前的代码:
<div ng-app="">
Send to Office: <input type="checkbox" ng-model="checked" ng-checked="true"><br/>
<select id="transferTo" ng-disabled="checked">
<option>Tech1</option>
<option>Tech2</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
这是一个jsfiddle:http://jsfiddle.net/hugmungus/LvHJw/5/
目前,页面加载并选中复选框,但未禁用列表.如果取消选中然后重新检查,则列表将被禁用.
谢谢您的帮助!
我是否认为在创建C#类SelectList后无法设置所选值?这不是有点傻吗?
(对不起,这里有几个项目,但似乎没有一个允许我让这个工作.)
我想创建一个允许多选的DropDownList.我能够填充列表,但我无法获得当前选定的值似乎工作.
我的控制器中有以下内容:
ViewBag.PropertyGroups = from g in db.eFinGroups
where g.GroupType.Contents == "P"
select new
{
Key = g.Key,
Value = g.Description,
Selected = true
};
ViewBag.SelectedPropertyGroups = from g in company.Entities
.First().Properties.First().PropertyGroups
select new {
g.eFinGroup.Key,
Value = g.eFinGroup.Description };
在视图中我有:
@Html.DropDownListFor(model => model.PropertyGroupsX,
new MultiSelectList(ViewBag.PropertyGroups
, "Key", "Value"
, ViewBag.SelectedPropertyGroups),
new { @class = "chzn-select", data_placeholder = "Choose a Property Group", multiple = "multiple", style = "width:350px;" })
PropertyGroupX是模型中的字符串[].
我已尝试使用所选属性的所有类型的迭代...只传递值,只传递键,两者等.
另外,PropertyGroupX应该是什么类型的?字符串数组是否正确?或者它应该是包含当前属性组的字典?我真的很难找到这方面的文档.
有人建议我应该使用ListBoxFor.我已经改变了,仍然有同样的问题.呈现选项标记时,所选值未设置为选中状态.这是我尝试过的:
@ Html.ListBoxFor(model => model.PropertyGroups,new MultiSelectList(ViewBag.PropertyGroups,"Key","Value"))
我已经尝试将model.PropertyGroups作为匹配值的字符串集合,作为Guid的集合匹配此ID,并作为匿名类型同时具有Key和Value以匹配ViewBag中的项目.似乎没什么用.
selectlist multi-select asp.net-mvc-3 drop-down-menu html.listboxfor
我正在扩展一个枚举,并给出以下代码,selectListItems是一个通用的SelectListItem列表,它具有我的枚举的所有正确值.
第一个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();
}
Run Code Online (Sandbox Code Playgroud) selectlist ×10
asp.net-mvc ×6
c# ×3
.net ×1
angularjs ×1
checkbox ×1
html-helper ×1
html5 ×1
ienumerable ×1
input ×1
jquery ×1
multi-select ×1
viewdata ×1