【问题标题】:Select from dropdownlist, chosen value create another same value?从下拉列表中选择,选择的值创建另一个相同的值?
【发布时间】: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


【解决方案1】:

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

看这里http://msdn.microsoft.com/en-us/library/dd492553(v=vs.118).aspx

你要选择的值,我必须作为 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)

【讨论】:

  • 这将是你的 GMTK 的 id 值试试吧
  • 我的意思是,如果我使用你的建议,错误就会显示出来。 selectedValue 带有下划线,表示无法解析符号 'slectedValue'
  • 什么是 selectedValue?
  • "Id","text" 应该代表什么,或者不是mather,你放在这里的是什么?因为在 Razor 中我收到此错误:DataBinding: 'System.String' does not contain a property with name 'Id'
  • "GMTK" 不是默认值,它是从数据库调用并设置为用户角色,这个 GMTK 仅适用于我测试过的这个默认用户,其他用户有不同的值。跨度>
【解决方案2】:

如果我没记错的话,您是在尝试去掉选项标签“--Select--”。 通过查看 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)

@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)

我无法在工作中测试它。但想法是,与其删除选项标签,不如尝试传递一个空值或空字符串,看看 API 是否会忽略它。

【讨论】:

  • 感谢您的建议,是的,我确实要取出标签--Select--,您的建议wokr,但不是我想要的,第一个值为空,我希望第一个值首先来自下拉列表...
  • @geekforfreek 如果这不起作用,您可能想看看this。他们设法为选项标签设置了一个值。因此,您可以将选项标签名称定义为您的第一个项目名称并相应地设置其值。
猜你喜欢
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多