我想知道,在MVC 4中创建下拉列表的最佳方法是什么?使用ViewBag或其他方法?
viewdata html-select html.dropdownlistfor viewbag asp.net-mvc-4
背景:我的页面上有4个下拉列表,它们都使用一个List来SelectListItem从中提取数据.这些下拉列表中的所有4个将始终具有相同的元素.每个下拉列表顶部都有一个空元素.
问题:如果我没有从列表中选择呈现第二个的项目,则在页面加载时,第二个列表会自动选择在第一个列表中选择的项目.这是因为(我认为)列表有一个SelectListItemwhere Selected = true,它只是在第二个列表中使用那个.
有没有办法为多个下拉列表使用一个列表源?我不想重复这个清单4次,除非我绝对要......
代码:
//this is the list source
public IEnumerable<SelectListItem> PossibleItems { get; set; }
//this is the code on my .cshtml page
@Html.DropDownListFor(x => x.SelectedItem1, Model.PossibleItems)
@Html.DropDownListFor(x => x.SelectedItem2, Model.PossibleItems)
@Html.DropDownListFor(x => x.SelectedItem3, Model.PossibleItems)
@Html.DropDownListFor(x => x.SelectedItem4, Model.PossibleItems)
Run Code Online (Sandbox Code Playgroud) 当我创建一个SelecList时,我希望能够手动添加SelecListItem并执行此操作,我使用以下代码:
List<SelectListItem> Provinces = new List<SelectListItem>();
Provinces.Add(new SelectListItem() { Text = "Northern Cape", Value = "NC" });
Provinces.Add(new SelectListItem() { Text = "Free State", Value = "FS" });
Provinces.Add(new SelectListItem() { Text = "Western Cape", Value = "WC" });
SelectList lstProvinces = new SelectList(Provinces);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
var lstProvinces = new SelectList(new[] { "Northern Cape", "Free State", "Western Cape" });
Run Code Online (Sandbox Code Playgroud)
在我创建SelectList之后,我通过ViewBag将它传递给DropDownListFor:
Html.DropDownListFor(m => m.StartPointProvince, (SelectList)ViewBag.Provinces)
Run Code Online (Sandbox Code Playgroud)
但是,当我使用第一种方法创建SelectList时,它不起作用 - 它将3个值添加到下拉列表中,但所有值显示为:
*输出截图
但是,当我使用第二种方法时,它工作正常.我希望使用第一种方法,因为我希望能够指定每个项目的Text AND值.
我使用我的枚举渲染一个下拉列表框,我只有3个选项,但由于某种原因它显示四个.top和default选项只是空白/空,我希望删除它.我希望top/default值为'Option1'.
枚举:
public enum EventType
{
[Display(Name = "Option 1")]
Option1,
[Display(Name = "Option 2")]
Option2,
[Display(Name = "Option 3")]
Option3
}
Run Code Online (Sandbox Code Playgroud)
视图:
@Html.EnumDropDownListFor(model => model.EventType, null, new { @id = "eventType", @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
我DropDownListFor用来在视图中呈现下拉列表.不知怎的,呈现的列表将不会选择SelectListItem与Selected设置为true.
在控制器动作中:
var selectList = sortedEntries.Select(entry => new SelectListItem
{
Selected = entry.Value.Equals(selectedValue),
Text = entry.Value,
Value = entry.Id
});
return View(new DropDownListModel
{
ListId = id,
SelectList = selectList,
OptionLabel = "Click to Select"
});
Run Code Online (Sandbox Code Playgroud)
在视图中:
<%= Html.DropDownListFor(m => m.ListId,
Model.SelectList,
Model.OptionLabel,
new {@class="someClass"}) %>
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
Selected设置为只有一个且只有一个项目true.SelectList在DropDownListFor: Html.DropDownListFor(m => m.ListId,
new SelectList(Model.SelectList, "Value", "Text",
new List<SelectListItem>(Model.SelectList).Find(s => s.Selected)),
new {@class="someClass"}) …Run Code Online (Sandbox Code Playgroud) asp.net-mvc selectlist selectlistitem html.dropdownlistfor drop-down-menu
我想在多选下拉框中为所选项目指定黄色.默认情况下,选择后它有灰色背景,我想这样做HTML, CSS.
任何人都可以帮忙吗?
通常我会将数据绑定到DropDownListFora SelectList:
@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, "OrderId", "ItemName"))
Run Code Online (Sandbox Code Playgroud)
有没有办法通过强类型lambdas而不是属性字符串来做到这一点.例如:
@Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.Orders, x => x.OrderId, x => x.ItemName))
Run Code Online (Sandbox Code Playgroud) PartialView
@model OsosYeni23072012.Models.TblMeters
<h3>
Model.customer_name
</h3>
<h3>
Model.meter_name
</h3>
Run Code Online (Sandbox Code Playgroud)
调节器
[HttpGet]
public ActionResult MeterInfoPartial(string meter_id)
{
int _meter_id = Int32.Parse(meter_id);
var _meter = entity.TblMeters.Where(x => x.sno == _meter_id).FirstOrDefault();
return PartialView("MeterInfoPartial", _meter);
}
Run Code Online (Sandbox Code Playgroud)
剃刀
@Html.DropDownList("sno", new SelectList(Model, "sno", "meter_name"), "-- Select Meter --", new { id = "meters"})
@Html.Partial("MeterInfoPartial")
Run Code Online (Sandbox Code Playgroud)
如果下拉列表更改,我想加载部分视图.但我不知道我怎么能这样做.我找不到任何关于此的例子.我用actionlink这样做.但我之前没有下拉.
controller参数meter_id等于dropdownlist selectedvalue.
谢谢.
我有这个工作和平的代码,显示在我的MVC3 Razor Web应用程序中填充DropDownList.
@Html.DropDownListFor(x => x.ReminderTime,
new SelectList(Model.RemindersList, "Item1 ", "Item2"))
Run Code Online (Sandbox Code Playgroud)
我需要保持DropDownList已填充但禁用它,因此用户无法在列表中选择值.
你能指出我正确的方向吗?请提供一些代码示例.谢谢!
我正在努力更新应用程序以使用Kendo UI,并且遇到了使用DropDownList绑定到Enum的问题.我遇到的两个问题是:1)该值不包含Enum值,而是包含"Today"(应为0),以及2)显示值始终为"Last10Days"而不是"Last 10 Days"标签.我看了,找不到另一个地方,有人使用Kendo UI将描述显示为文本,并包含数值而不是文本.任何帮助表示赞赏.
<div class="span6">
@Html.LabelFor(m=> m.DateRanges)
@(Html.Kendo().DropDownListFor(m => m.DateRanges)
.BindTo(Enum.GetNames(typeof(SearchDateRanges)).ToList())
.HtmlAttributes(new { value = "Today" })
.DataTextField("Text")
.Events(e => e.Change("DateChange")))
</div>
<div class="span6">
@Html.LabelFor(m => m.Status)
@(Html.Kendo().DropDownListFor(m=> m.Status)
.BindTo(Enum.GetNames(typeof(SearchStatusCriteria)).ToList())
.HtmlAttributes(new {value = "All"}))
</div>
Run Code Online (Sandbox Code Playgroud)
public enum SearchDateRanges
{
[Description("Today")]
Today = 0,
[Description("Last 10 Days")]
Last10Days = 1,
/// <summary>
/// The last 30 days.
/// </summary>
[Description("Last 30 Days")]
Last30Days = 2,
[Description("Last 60 Days")]
Last60Days = 3,
[Description("Last 90 Days")]
Last90Days = 4, …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×7
c# ×4
asp.net ×2
razor ×2
selectlist ×2
css ×1
html-helper ×1
html-select ×1
jquery ×1
kendo-ui ×1
multi-select ×1
viewbag ×1
viewdata ×1