简体   繁体   English

加载 HTML 时出现 MVC 错误。 具有键“MY KEY”的 ViewData 项的类型为“System.String”,但必须为“IEnumerable”类型<selectlistitem> '</selectlistitem>

[英]The MVC error on load HTML. ViewData item that has the key 'MY KEY' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

I am doing an MVC APP.我正在做一个 MVC APP。 For this example..对于这个例子..

In my definition of my DropDownListFor I defined something like this.在我对DropDownListFor的定义中,我定义了这样的内容。

  @Html.DropDownListFor(model => model.SelectedSystem, Model.Systems, new { @class = "form-control listbox",id="Origin" })

My Model is loaded in the Controller, where it loads Model.System under certain circumstances.我的 Model 被加载到 Controller 中,它在某些情况下加载Model.System Model.System is of type List<SelectListItem> Model.System 是List<SelectListItem>类型

The option selected is in model.SelectedSystem that is a string type .选择的选项在model.SelectedSystem中,它是一个string type That works fine...这很好用...

The problem i am facing is when Model.System is null.我面临的问题是Model.System是 null 时。

My Controller looks like this我的 Controller 看起来像这样

public ActionResult Index()
        {
            var apiEntitySyncViewModel = new ApiEntitySyncViewModel
            {
                Systems = _entitySyncService.GetEnvironments(),
            };
            return View(apiEntitySyncViewModel);
        }

On running appears the message The ViewData item that has the key SelectedSystemOrigin is of type System.String but must be of type IEnumerable<SelectListItem>运行时出现消息The ViewData item that has the key SelectedSystemOrigin is of type System.String but must be of type IEnumerable<SelectListItem>

How can I draw an empty DropDownListFor without having that mistake如何绘制一个空的 DropDownListFor 而不会出现该错误

try this尝试这个

public ActionResult Index()
{
   var apiEntitySyncViewModel = new ApiEntitySyncViewModel
   {
       Systems = _entitySyncService.GetEnvironments(),
  };
if(apiEntitySyncViewModel.Systems==null) apiEntitySyncViewModel.Systems = new List <SelectListItem>();

//or you can try
new List <SelectListItem>{ new SelectListItem{ Value="0", Text ="Empty" } 


  return View(apiEntitySyncViewModel);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有类型为“ IEnumerable”的ViewData项 <SelectListItem> &#39;有钥匙&#39;...&#39; - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '…' 关键字为“ JobTitleID”的ViewData项的类型为“ System.Int32”,但必须类型为“ IEnumerable” <SelectListItem> “? - The ViewData item that has the key 'JobTitleID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'? InvalidOperationException:没有“IEnumerable”类型的 ViewData 项<selectlistitem> ' 有键 'HospitalId'</selectlistitem> - InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'HospitalId' ASP.NET MVC - HTML 错误“没有 IEnumerable 类型的 ViewData<selectlistitem> ”</selectlistitem> - ASP.NET MVC - HTML Error “No ViewData of Type IEnumerable<SelectListItem>” 如何使用ViewData在MVC中加载HTML表? - How to load HTML Table in MVC using ViewData? 使用JavaScript将值返回到html时,Solidity map(int,string)出现函数错误。 - Function error with my Solidity map(int,string) while using javascript to return values to html. HTML keyup / keydown 有很多检测键类型的方法。 我应该使用哪个? - HTML keyup / keydown has a lot of ways to detect key type. Which should I use? MVC:将类型为&#39;double&#39;的ViewData对象传递给数字输入字段 - MVC: Pass ViewData object with type 'double' to number input field Umbraco MVC构建错误-Collection.cshtml-&#39;System.Web.WebPages.Html.HtmlHelper&#39;不包含&#39;ViewData&#39;或&#39;EditorFor&#39;的定义 - Umbraco MVC build error - Collection.cshtml - 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ViewData' or 'EditorFor' send-mailmessage用System.String []替换html样式标签。 - send-mailmessage replaces html style tags with System.String[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM