没有类型为'IEnumerable <selectlistitem>'的ViewData项具有键'Department'</selectlistitem>

时间:2015-03-20 08:56:01

标签: c# model-view-controller dropdownlistfor

我确实检查了所有相关问题,但我无法弄清楚如何解决它。我需要你的帮助。

这是关于下拉列表的代码。当我发布页面时,我收到了上面提到的错误消息。

.cshtml

 <div class="editor-label">
                    @Html.LabelFor(model => model.Department, new { @style = " margin-top:12px;display:block;text-align:center" })
                </div>
                <div class="editor-field">
                    @Html.DropDownList("Department",(SelectList) ViewBag.Department, new { id = "Department", @class = "form-control", @style = "width:250px; margin-top:5px;margin-left: auto;margin-right:auto; text-align:center;" })
                    @Html.ValidationMessageFor(model => model.Department.Name)
                </div>

模型

public ActionResult Index()
    {
        IEnumerable<SelectListItem> items = db.Department
          .Select(c => new SelectListItem
          {
              Value = c.ID.ToString(),
              Text = c.Name
          });
        var selectList = new SelectList(items,"Value","Text");
        ViewBag.Department = selectList;
        return View();
    }


     [HttpPost]
     public ActionResult Index(Person model, HttpPostedFileBase photo)
     {
         if (ModelState.IsValid)
         {
             Person Person = new Person();
             Person.DeparmentID = model.Department.ID;
             Person.FirmID = model.Firm.ID;
             Person.GraduationDate = model.GraduationDate;
             Person.HomeTel = model.HomeTel;
             Person.MobileTel = model.MobileTel;
             Person.Mail = model.Mail;
             Person.Name = model.Name;
             Person.Surname = model.Surname;
             Person.Position = model.Position;
             Person.WorkingSituation = model.WorkingSituation;
             if (photo != null && photo.ContentLength > 0)
             {

                 if (photo.ContentLength > 10240)
                 {
                     ModelState.AddModelError("photo", "Resim boyutu 10 KB'ı aşamaz.");
                     return View();
                 }

                 var supportedTypes = new[] { "jpg", "jpeg", "png" };

                 var fileExt = System.IO.Path.GetExtension(photo.FileName).Substring(1);

                 if (!supportedTypes.Contains(fileExt))
                 {
                     ModelState.AddModelError("photo", "Yalnızca jpg, jpeg, png veri tipleri desteklenmektedir.");
                     return View();
                 }

                 var fileName = Path.GetFileName(photo.FileName);
                 photo.SaveAs(Server.MapPath("~/Upload/") + photo.FileName);
                 Person.Img = fileName;
             }
             db.Person.Add(Person);
             db.SaveChanges();
             ViewBag.ShowConfirmation = "The item was created.";

             return RedirectToAction("Index");
         }
         else
         {
             return View(model);
         }

     }

1 个答案:

答案 0 :(得分:1)

也许你使用DropDownListFor而不是DropDownList?您需要在viewmodel中而不是在viewbag中包含listitems。

沿着这些方向

@Html.DropDownListFor(m => m.Department, new SelectList(Model.Departments, "Value", "Text", Model.Department), new { @style = "..." })

SelectList构造函数的第二个和第三个参数定义SelectListItem列表中Value和Text参数的名称。