[英]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.