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

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

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

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

问题如下:

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

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

 [HttpPost]
public ActionResult AddCorporation(CorpModel corp)
{
var currentUser = HttpContext.Session[Constants.USER_KEY] as USP.Models.POCO.User; if (ModelState.IsValid)
{
AjaxResult result = sysCorpBll.AddCorporation(corp.CorpName, corp.CorpType, currentUser.SysCorp.ID);
if (result.flag)
{
return RedirectToAction("Corporation", "Corp");
}
}
var temp = dropDownListService.InitCorpTypeDropDownList11();
            ViewData["Types"] = new SelectList(temp, "ID", "Name"); ; return View("AddCorporation", corp);
}

第二次就报上面异常了

最终的解决方案

再viewmodel中增加

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

    public  class CorpModel
{
[Required]
[Display(Name = "公司名称")]
public string CorpName { get; set; }
[Required]
[Display(Name = "公司类型")]
public long CorpType { get; set; } public IEnumerable<SelectListItem> CorpTypeList { get; set; }
}
  [HttpGet]
public ActionResult AddCorporation()
{
//var temp = dropDownListService.InitCorpTypeDropDownList11(); //ViewData["Types"] = new SelectList(temp, "ID", "Name"); ;
var temp = dropDownListService.InitCorpTypeDropDownList11();
CorpModel corptype = new CorpModel();
corptype.CorpTypeList = new SelectList(temp, "ID", "Name");
return View(corptype);
} [HttpPost]
public ActionResult AddCorporation(CorpModel corp)
{
var currentUser = HttpContext.Session[Constants.USER_KEY] as USP.Models.POCO.User; if (ModelState.IsValid)
{
AjaxResult result = sysCorpBll.AddCorporation(corp.CorpName, corp.CorpType, currentUser.SysCorp.ID);
if (result.flag)
{
return RedirectToAction("Corporation", "Corp");
}
}
var temp = dropDownListService.InitCorpTypeDropDownList11();
corp.CorpTypeList= new SelectList(temp, "ID", "Name"); return View("AddCorporation", corp);
}
        <div class="form-group">
@Html.LabelFor(model => model.CorpType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(u => u.CorpType,Model.CorpTypeList , "-- Select Contact --", new { @class = "form-control", required = "true" })
@Html.ValidationMessageFor(model => model.CorpType, "", new { @class = "text-danger" })
</div>
</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. golang sqrt error练习

      练习:错误 从先前的练习中复制 Sqrt 函数,并修改使其返回 error 值. 由于不支持复数,当 Sqrt 接收到一个负数时,应当返回一个非 nil 的错误值. 创建一个新类型 type Er ...

  2. Java设计模式(14)责任链模式(Chain of Responsibility模式)

    Chain of Responsibility定义:Chain of Responsibility(CoR) 是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合, ...

  3. 记录一下寄几个儿的greendao数据库升级,可以说是非常菜鸡了嗯

    之前使用的greendao数据库存储服务器所有的历史推送消息,但是后来消息需要加几个新的字段 举个栗子,比如要新增红色框住的字段到数据库中: 本仙女作为一只思想成熟的菜鸡,当然是加了字段就赶紧重新往里 ...

  4. Ubuntu 16.04 标题栏实时显示上下行网速、CPU及内存使用率

    有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...

  5. Linux编程_Shell脚本练习题

    1,编写shell脚本,计算1~100的和. #! /bin/bash `;do sum=$[$i+$sum] done echo $sum 2,编写shell脚本,输入一个数字n并计算1~n的和. ...

  6. SpringMVC系列(十二)自定义拦截器

    Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口– preHandle():这个方法在业务处 ...

  7. unity3d-----Collider 组件参考

    Collider 组件参考 点击 属性检查器 下面的 添加组件 按钮,然后从 添加碰撞组件 中选择需要的 Collider 组件,即可添加 Collider组件到节点上. Collider 组件属性 ...

  8. php可选缓存APC

    1.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓 ...

  9. HttpURLConnection 发送 文件和字符串信息

    以文件的形式传参/**     * 通过拼接的方式构造请求内容,实现参数传输以及文件传输     *      * @param actionUrl 访问的服务器URL     * @param pa ...

  10. HTTP之referer

    安全培训中提到可以通过referer判断安全性,hackbar中也有一个enable referer的选项,则,这个referer到底是个什么角色? (以下是搜集的一些资料整合,链接均放到底部,不再一 ...