Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 从下拉列表中选择,选择的值是否创建另一个相同的值?_Asp.net_Asp.net Mvc 4_Drop Down Menu - Fatal编程技术网

Asp.net 从下拉列表中选择,选择的值是否创建另一个相同的值?

Asp.net 从下拉列表中选择,选择的值是否创建另一个相同的值?,asp.net,asp.net-mvc-4,drop-down-menu,Asp.net,Asp.net Mvc 4,Drop Down Menu,我创建了下拉列表,使用此控制器: string tmpUser = System.Web.HttpContext.Current.User.Identity.Name; var kdoseprijavlja = (from dllist in dbContext.UsersTables where dllist.username == tmpUser select dllist.userID).FirstOr

我创建了下拉列表,使用此控制器:

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;
这就产生了:

所以它会这样做,当我选择一些值,并点击搜索后它保存值,我选择,这是好的,但

我的问题是,我不想让dropdownlist没有选项-Select-,所以第一个值应该是,dropdownlist的第一个值。我已经这样做了,我已经从索引中删除了-Select-from:

@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并单击“搜索”,它会这样做:它会使值加倍


在控制器部分,可以在viewbag中添加选定值

看这里

要选择的值必须作为selectedValue传递给我

string selectedValue = "GMTK";
ViewBag.DropList = new SelectList(result,"Id","text",selectedValue);
希望它能帮助你。祝你今天愉快

或者在Razor中更正DropDownList的最后一部分:

 @Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), (SelectList)ViewBag.DropList)

如果我没有错的话,您正在尝试将选项label-Select-out去掉。 通过查看API,显然选项标签接受空值。你试过类似的东西吗

@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), "", Model.Search.Destination)


我不能在工作中测试它。但是,我们的想法是,不要删除选项标签,而是尝试传递空值或空字符串,看看API是否会忽略它。

通过默认设置第一个选项它将是GMTK try的id值。我的意思是,如果我使用您的建议,错误将显示。selectedValue是下划线,表示无法解析符号“slectedValue”selectedValue是什么?Id、文本代表什么,或者它不是mather,您在这里放的是什么?因为在Razor中,我得到了以下错误:DataBinding:'System.String'不包含名为'Id'的属性GMTK不是默认值,它是从数据库调用的,并设置为用户角色,此GMTK仅适用于我测试过的默认用户,其他用户有不同的值。感谢您的建议,是的,我不想去掉label-Select-,你的建议很好,但不是像我说的那样,第一个值是空的,我说第一个值是dropdownlist中的第一个…@geekforfreek如果这不起作用,你可能想看看。他们设法为选项标签设置了一个值。因此,您可以将选项标签名称定义为第一个项目名称,并相应地设置其值。
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), "", Model.Search.Destination)
@Html.DropDownList("destination", ((SelectList)ViewBag.DropList).Select(t => new SelectListItem() { Text = t.Text, Value = t.Text, Selected = (t.Text == ViewBag.Destination) }), string.Empty, Model.Search.Destination)