【发布时间】:2014-06-27 17:51:03
【问题描述】:
我已经用这个控制器创建了下拉列表:
string tmpUser = System.Web.HttpContext.Current.User.Identity.Name;
var kdoseprijavlja = (from dllist in dbContext.UsersTables
where dllist.username == tmpUser
select dllist.userID).FirstOrDefault();
var seznam = dbContext.CustomerTables.Where(m => m.UserId == kdoseprijavlja).Select(m => m.Customer);
var seznam2 = dbContext.CustomTypes.Where(m => seznam.Contains(m.Id_NewCustomerType)).Select(m => m.CustomerType);
List<string> result = new List<string>();
foreach (var customer in seznam2)
{
result.Add(customer);
}
ViewBag.DropList = new SelectList(result);
ViewBag.Destination = parameters.Destination;
在视图索引中我创建了这个:
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), "--Select--", Model.Search.Destination)
现在创建这个:
所以它这样做,当我选择一些值,然后点击搜索(发布)它保存我选择的值,这很好,但是!
我的问题是,我想要没有选项“--Select--”的下拉列表,所以第一个值是下拉列表中的第一个值。我已经这样做了,我已经删除 - -Select-- 从索引:
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), Model.Search.Destination)
从代码中删除 --Select-- 有效,但问题是选择的值加倍,例如,如果我选择“GMTK”并点击搜索它这样做:(它加倍值)
【问题讨论】:
-
设置默认选择的第一个
标签: asp.net asp.net-mvc-4 drop-down-menu