严重禁止转载,好多爬虫软件为了浏览到处抓东西,真缺德

具有键“CorpType”的 ViewData 项属于类型“System.Int64”,但它必须属于类型“IEnumerable<SelectListItem>"

先吐槽,不是不依赖微软的东西,是我对微软的类库实在有种排斥感,有些东西错了又找不到原因,这个问题就找了5个小时

网上硬是没有解决办法,因为技术总监的缘故就得照着做,无奈~~~~~~

问题如下:

  1. public class CorpModel
  2. {
  3. [Required]
  4. [Display(Name = "公司名称")]
  5. public string CorpName { get; set; }
  6. [Required]
  7. [Display(Name = "公司类型")]
  8. public long CorpType { get; set; }
  9. }
  1. public ActionResult AddCorporation()
  2. {
  3. var temp = dropDownListService.InitCorpTypeDropDownList11();
  4. ViewData["Types"] = new SelectList(temp, "ID", "Name"); ;
  5. return View(corptype);
  6. }
  1. <div class="form-group">
  2. @Html.LabelFor(model => model.CorpType, htmlAttributes: new { @class = "control-label col-md-2" })
  3. <div class="col-md-10">
  4. @Html.DropDownListFor(u => u.CorpType,Model.CorpTypeList , "-- Select Contact --", new { @class = "form-control", required = "true" })
  5. @Html.ValidationMessageFor(model => (SelectList)ViewData["Types"], "", new { @class = "text-danger" })
  6. </div>
  7. </div>

第一次调用的时候是没问题的

  1. [HttpPost]
  2. public ActionResult AddCorporation(CorpModel corp)
  3. {
  4. var currentUser = HttpContext.Session[Constants.USER_KEY] as USP.Models.POCO.User;
  5.  
  6. if (ModelState.IsValid)
  7. {
  8. AjaxResult result = sysCorpBll.AddCorporation(corp.CorpName, corp.CorpType, currentUser.SysCorp.ID);
  9. if (result.flag)
  10. {
  11. return RedirectToAction("Corporation", "Corp");
  12. }
  13. }
  14. var temp = dropDownListService.InitCorpTypeDropDownList11();
                ViewData["Types"] = new SelectList(temp, "ID", "Name"); ;
  15.  
  16. return View("AddCorporation", corp);
  17. }

第二次就报上面异常了

最终的解决方案

再viewmodel中增加

public IEnumerable<SelectListItem> CorpTypeList { get; set; }

  1. public class CorpModel
  2. {
  3. [Required]
  4. [Display(Name = "公司名称")]
  5. public string CorpName { get; set; }
  6. [Required]
  7. [Display(Name = "公司类型")]
  8. public long CorpType { get; set; }
  9.  
  10. public IEnumerable<SelectListItem> CorpTypeList { get; set; }
  11. }
  1. [HttpGet]
  2. public ActionResult AddCorporation()
  3. {
  4. //var temp = dropDownListService.InitCorpTypeDropDownList11();
  5.  
  6. //ViewData["Types"] = new SelectList(temp, "ID", "Name"); ;
  7. var temp = dropDownListService.InitCorpTypeDropDownList11();
  8. CorpModel corptype = new CorpModel();
  9. corptype.CorpTypeList = new SelectList(temp, "ID", "Name");
  10. return View(corptype);
  11. }
  12.  
  13. [HttpPost]
  14. public ActionResult AddCorporation(CorpModel corp)
  15. {
  16. var currentUser = HttpContext.Session[Constants.USER_KEY] as USP.Models.POCO.User;
  17.  
  18. if (ModelState.IsValid)
  19. {
  20. AjaxResult result = sysCorpBll.AddCorporation(corp.CorpName, corp.CorpType, currentUser.SysCorp.ID);
  21. if (result.flag)
  22. {
  23. return RedirectToAction("Corporation", "Corp");
  24. }
  25. }
  26. var temp = dropDownListService.InitCorpTypeDropDownList11();
  27. corp.CorpTypeList= new SelectList(temp, "ID", "Name");
  28.  
  29. return View("AddCorporation", corp);
  30. }
  1. <div class="form-group">
  2. @Html.LabelFor(model => model.CorpType, htmlAttributes: new { @class = "control-label col-md-2" })
  3. <div class="col-md-10">
  4. @Html.DropDownListFor(u => u.CorpType,Model.CorpTypeList , "-- Select Contact --", new { @class = "form-control", required = "true" })
  5. @Html.ValidationMessageFor(model => model.CorpType, "", new { @class = "text-danger" })
  6. </div>
  7. </div>

这样就可以避免这种问题了。。。

在前边用了一个Html.DropDownListFor方法渲染控件

结果报异常,用reflector跟了半天,结果调用了MVCForm的IDispose方法,我也是醉了

有一个页面用了同样的方法去调用,没问题,到这里就有了问题,我想了想因为是Dispose了顺便又调用了GC,所以页面要重新加载

所以在调用View的时候就得重新赋值给这里的return View("AddCorporation", corp);是没有DropDownList的数据,以至于报错

DropDownListFor的种种纠结(禁止转载)的更多相关文章

  1. 【原创】复制知乎“禁止转载”的内容做笔记 - 基于oncopy监听器的简单解决方案

    原理:移除所有oncopy的监听器. 使用: 新建书签,地址设为: javascript: getEventListeners(document).copy.forEach(({listener}) ...

  2. angularjs过滤器(一)------禁止转载------

    如果用代码实现:将new Date()转换为 M/D/YY 00:00 格式. 启用过滤器有两种方式:①在HTML中用"|"启用过滤器.格式为{{$scope.property | ...

  3. 原创:LNMP架构部署个人博客网站 禁止转载复制

    nginx编译安装步骤 ①. 检查软件安装的系统环境 cat /etc/redhat-release uname -r ②. 安装nginx的依赖包(pcre-devel openssl-devel) ...

  4. SQLyog试用到期的解决方法(仅供个人学习使用,禁止转载或用于商业盈利)

    作者:EzrealYi 本章链接:https://www.cnblogs.com/ezrealyi/p/12434105.html win+r->输入regedit->进入注册表 在计算机 ...

  5. 动词 or 名词 :这是一个问题 【转载】

    前言:有网友让我用通俗的语言来讲一讲RESTful ,   我在这一块工程实践的不太多,有点为难了,  只能讲一讲我的理解, 欢迎大家批评指正.计算机行业最擅长造新词了,像什么AJAX,IoC, AO ...

  6. [转载] TLS协议分析 与 现代加密通信协议设计

    https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...

  7. 【转载】在linux下别用zip 用tar来压缩文件 zip解压后还是utf-8 window10是GBK

    3.2 使用 unzip 命令解压缩 zip 文件 将 shiyanlou.zip 解压到当前目录:   $ unzip shiyanlou.zip 使用安静模式,将文件解压到指定目录:   $ un ...

  8. 【转载】不得不知道的Python字符串编码相关的知识

    原文地址:http://www.cnblogs.com/Xjng/p/5093905.html 开发经常会遇到各种字符串编码的问题,例如报错SyntaxError: Non-ASCII charact ...

  9. 【转载】SOCKS代理:从***到内网漫游

    原文:SOCKS代理:从***到内网漫游 本文原创作者:tahf,本文属FreeBuf原创奖励计划,未经许可禁止转载 之前在Freebuf上学习过很多大牛写的关于Tunnel.SOCKS代理.***等 ...

随机推荐

  1. Servlet 全局验证是否登录

    Java过滤器的使用和拦截排除的实现 http://blog.csdn.net/kyunchen/article/details/52187514

  2. Navi.Soft31.阅读导航

    Navi.Soft31.核心类库 Navi.Soft31.WinForm框架 Navi.Soft31.WebMVC框架 Navi.Soft31.微信WinForm框架 Navi.Soft31.Mobi ...

  3. android 在HTML中显示bitmap

    逻辑:将bitmap转化为Base64,通过调用HTML中的JS,显示到HTML中 (1)android代码 public String bitmaptoString(Bitmap bitmap) { ...

  4. List<Map<String, Object>>是什么意思

    List集合中的对象是一个Map对象,而这个Map对象的键是String类型,值是Object类型 List以Map接口对象为列表对象. Map以String为键,以Object为值. List里只能 ...

  5. ubuntu安装mxnet GPU版本

    安装mxnet GPUsudo pip install mxnet-cu80==1.1.0 推荐pip安装mxnet,土豪gpu版本: pip install mxnet-cu90==1.0.0 豪华 ...

  6. Kb,KB,Kbps,Mb,Mbps等一些列概念

    先普及一下Kb,KB,Kbps,Mb,Mbps等一些列概念 1Byte = 8bit1KB (Kilobyte 千字节)=1024Byte1MB (Megabyte,兆字节,简称“兆”)=1024KB ...

  7. e858. 将键盘键和事件绑定

    This example creates a number of keystrokes and adds them to the input map of a component. When a ke ...

  8. http 状态码 40x

    400 无法解析此请求. 401.1 未经授权:访问由于凭据无效被拒绝. 401.2 未经授权: 访问由于服务器配置倾向使用替代身份验证方法而被拒绝. 401.3 未经授权:访问由于 ACL 对所请求 ...

  9. Python操作SQLServer示例(转)

    转自:http://www.cnblogs.com/lrzy/p/4346781.html 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数 ...

  10. Xianfeng轻量级Java中间件平台:功能管理

    功能管理:从功能类型来看,不属于用户使用的功能,而属于系统维护使用的功能,因为对于用户来说,是不可见的.功能管理的作用是定义一套规则,用来处理权限等业务,比如常见的菜单权限控制.按钮权限控制等情景 在 ...