报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>"
报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>"
□ 背景分析
MVC控制器中有2个方法,一个是用来显示添加视图,另一个是用来接收添加数据。
public ActionResult Add()
{
ViewData["o"] = 类型为List<SelectListItem>的集合实例;
return View();
}
[HttpPost]
public ActionResult Add(SomeViewModel model)
{
if(ModelState.IsValid)
{
//TO DO
}
else
{
return View(model);
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ 解决方法
控制器方法Add()在接收视图传来的view model,在验证失败的情况下,返回到显示添加的视图,这时,同样需要把类型为List<SelectListItem>的集合实例传递到前台视图。正确的写法是:
[HttpPost]
public ActionResult Add(SomeViewModel model)
{
if(ModelState.IsValid)
{
//TO DO
}
else
{
ViewData["o"] = 类型为List<SelectListItem>的集合实例;
return View(model);
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
报错:具有键"..."的ViewData项属于类型"...",但它必须属于类型"IEnumerable<SelectListItem>"的更多相关文章
- SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...
- IDEA报错 Unable to open debugger port (127.0.0.1:63342): java.net.BindException "Address already in use
Unable to open debugger port (127.0.0.1:63342): java.net.BindException "Address already in use ...
- javaweb 拦截器报错
拦截器报错 The content of element type "interceptor-ref" must match "(param)*".内容元素 ...
- spring加载bean报错:expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
看具体报错日志: 警告: Unable to proxy interface-implementing method [public final void cn.wlf.selection.proto ...
- spark读取pg数据库报错操作符不存在
代码: Properties connectionProperties = new Properties(); connectionProperties.put("user", C ...
- python中如何通过报错信息定位问题(异常传播轨迹)
class SelfException(Exception): pass def main(): firstMethod() def firstMethod(): secondMethod() def ...
- Eclipse开发Android项目报错解决方案详细教程,最新版一篇就够了!
本文记录刚接触Android开发搭建环境后新建工程各种可能的报错,并亲身经历漫长的解决过程(╥╯^╰╥),寻找各种偏方,避免大家采坑,希望能帮助到大家. 报错信息 出错一:The import and ...
- vs2015 安装之后安装MSSM 2016 导致 vs启动报错 System.ArgumentException 已添加了具有相同键的项,ActivityLog.xml
如题,先是装了vs2015,开发什么的都没有问题,后来安装了SqlServer2016 MSSM,出大问题了,vs2015打开就报错,具体错误如上,还想还有个ActivityLog.xml 这个文件的 ...
- 不存在具有键“xxxId”的“IEnumerable<SelectListItem>”类型的 ViewData 项
项目中的某个页面,在访问时出现以下错误: 不存在具有键“xxxId”的“IEnumerable<SelectListItem>”类型的 ViewData 项 具体的场景说明如下: 一个编辑 ...
随机推荐
- malloc和free的实现
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- 20165203《Java程序设计》第三周学习总结
教材学习内容总结 1.类: (1)类的声明:class+类名 (2)类体:成员变量的声明+方法(局部变量+语句) 注意: 方法体内声明的局部变量只在方法内有效和书写位置有关. 局部变量和成员变量同名: ...
- 使用mongo-java-driver-3.0.2连接MongoDB数据库
这里使用的mongodb的java驱动版本是:3.0.2,文件名mongo-java-driver-3.0.2.jar 博客本地下载下载网址(也可以下载其它版本):http://central.ma ...
- python开发学习-day16(Django框架初识)
s12-20160507-day16 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- USACO 4.3 Letter Game (字典树)
Letter GameIOI 1995 Figure 1: Each of the 26 lowercase letters and its value Letter games are popula ...
- Django实战(19):自定义many-to-many关系,实现Atom订阅
记得有人跟我说过,rails的has_many :through是一个”亮点“,在Django看来,该功能简直不值一提.rails中的many-to-many关联中,还需要你手工创建关联表(写 mig ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
- <泛> C++3D数学库设计详解 向量篇
// 注:本内容为作者原创,禁止在其他网站复述内容以及用于商业盈利,如需引用,请标明出处:http://www.cnblogs.com/lv_anchoret/ Preface 为了支持光线追踪的学习 ...
- [python 源码]字符串对象的实现
还是带着问题上路吧,和整数对象的实现同样的问题: >>> a='abc' >>> b='abc' >>> a is b True >> ...
- 【WIN10】程序內文件讀取與保存
DEMO下載:http://yunpan.cn/cFHIZNmAy4ZtH 访问密码 cf79 1.讀取與保存文件 Assets一般被認為是保存用戶文件數據的地方.同時,微軟還支持用戶自己創建文件夾 ...