• DropDownListFor的种种纠结(禁止转载)


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

    具有键“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的数据,以至于报错

  • 相关阅读:
    最大子数组
    链表插入排序
    链表求和
    有效回文串
    排球比赛计分系统
    超市收银系统
    三层架构
    Wpf+数据库代码封装+策略模式封装
    封装逻辑用策略模式实现
    代码封装
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/5143754.html
Copyright © 2020-2023  润新知