Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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
C# MVC/C:DropDownList用于不选择“已选”项目_C#_Asp.net Mvc_Dictionary_Html.dropdownlistfor_Selectlistitem - Fatal编程技术网

C# MVC/C:DropDownList用于不选择“已选”项目

C# MVC/C:DropDownList用于不选择“已选”项目,c#,asp.net-mvc,dictionary,html.dropdownlistfor,selectlistitem,C#,Asp.net Mvc,Dictionary,Html.dropdownlistfor,Selectlistitem,好的,我有一种情况,我需要有多个使用同一字典的“BayTypes”DDL,这不是问题。“n”个选项各有一个DDL。我将字典作为“BayTypes”传递给我的视图,如下所示: 控制器 var bayTypes = _bayTypeRepository.GetBayTypes().ToList(); property.BayTypes = bayTypes.ToDictionary(g => g.Name, g => g.BayTypeGuid.ToString()); 看法 在“结果

好的,我有一种情况,我需要有多个使用同一字典的“BayTypes”DDL,这不是问题。“n”个选项各有一个DDL。我将字典作为“BayTypes”传递给我的视图,如下所示:

控制器

var bayTypes = _bayTypeRepository.GetBayTypes().ToList();
property.BayTypes = bayTypes.ToDictionary(g => g.Name, g => g.BayTypeGuid.ToString());
看法

在“结果”对象中选择了正确的项。如果我一步一步地看“结果”,我可以看到正确的结果是“Selected=true”。。。但当它呈现时,它不会在DDLFor中选择


我缺少什么?

最终,决定下拉列表中所选项目的是ModelState,而不是SelectListItem.selected属性。ModelState由以下来源组成:请求、ViewData、ViewBag和最终模型

检查Request[BayTypes]、ViewData[BayTypes]、ViewBag.BayTypes和Model.BayTypes的值。如果其中任何一个值与您希望选择的值不同,这就是您的问题,尤其是如果该值甚至不在大致范围内


例如,造成这种情况的一个常见原因是开发人员将其实际选择列表存储在类似ViewBag.Foo的内容中,然后尝试将其应用到绑定到Model.Foo的下拉列表中。此时选择列表本身将成为ModelState中的选定项,而不是您选择的某个特定值。

解决了它。。。更改为.DropDownList no“For”,并将“name”作为先前创建的“result”变量传入。工作

var overrideValue = item.BayTypeOverride ? item.BayTypeOverrideValue.BayTypeGuid.ToString() : string.Empty;
var result = (from x in Model.BayTypes
              select new SelectListItem()
              {
                Text = x.Key,
                Value = x.Value,
                Selected = x.Value == overrideValue
               });
               if (item.BayTypeOverride)
               {
                @Html.DropDownList("result", result, htmlAttributes: new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue" })
               }
               else
               {
                @Html.DropDownList("result", result, htmlAttributes: new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue", @style = "display:none;" })
                                }

在这个解决方案的其他地方,我使用了您上面描述的Viewbag模式,但它的工作方式略有不同。哈哈,这不是一个绑定字段。我只传入一个字典,而不是“BayType”对象的列表,并通过Viewmodel重新映射回BayType/BayOptions对象。我将检查您建议查看的值。虽然你没有提到“修复”。谢谢。修复取决于您找到的内容。例如,如果要将某些内容设置为具有类似名称的ViewBag属性,则只需更改该属性的名称即可。然而,如果您从请求参数中获得了一些东西,那么您可能需要以不同的方式进行调整。这一切都要看情况而定。问题实际上是存在“n”个BayOptions,因此我无法在控制器中设置所选的,它必须在view per BayOptions中设置。
var overrideValue = item.BayTypeOverride ? item.BayTypeOverrideValue.BayTypeGuid.ToString() : string.Empty;
var result = (from x in Model.BayTypes
              select new SelectListItem()
              {
                Text = x.Key,
                Value = x.Value,
                Selected = x.Value == overrideValue
               });
               if (item.BayTypeOverride)
               {
                @Html.DropDownList("result", result, htmlAttributes: new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue" })
               }
               else
               {
                @Html.DropDownList("result", result, htmlAttributes: new { @Name = "BayOptionsToSubmit[" + aCounter + "].BayTypeOverrideValue", @style = "display:none;" })
                                }