具有键“KEY”的ViewData项是“System.Int32”,但必须是“IEnumerable <selectlistitem>”类型

时间:2016-04-13 14:32:59

标签: asp.net-mvc html.dropdownlistfor

具有键“roleID”的ViewData项是“System.Int32”,但必须是“IEnumerable”类型。

我无法解决它 我需要你的帮助!!这是我的代码:

admin模特:

 [Key]
    public int adminID { get; set; }

    [DisplayName("Role")]
    public int roleID { get; set; }
    public virtual Role roles { get; set; }

    [Required(ErrorMessage ="Le nom est obligatoire.")]
    public string Nom { get; set; }

    [Required(ErrorMessage = "Le prenom est obligatoire.")]
    public string Prenom { get; set; }

    [Required(ErrorMessage = "L'email est obligatoire.")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "email n'est pas valide.")]
    [DataType(DataType.EmailAddress)]
    [Remote("IsEmailAdminExist", "Admin", ErrorMessage = "URL exist!")]
    public virtual  string Email { get; set; }

    [Required(ErrorMessage = "Le mot de pass est obligatoire.")]
    [DataType(DataType.Password)]
    public string Pass { get; set; }

    public string Photo { get; set; }

    [NotMapped]
    public HttpPostedFileBase PhotoUpload{ get; set; }

角色模型:

[Key]
public int roleID { get; set; }

[Required(ErrorMessage = "Le nom est obligatoire.")]
public string Nom { get; set; }

public virtual ICollection<Admin> Admins { get; set; }

管理员控制器:

GET:

// GET: Admin/Edit/5
            public ActionResult Edit(int id)
            {
                var data = from p in AdminContext.roles
                           select new
                           {
                               roleID = p.roleID,
                               roleNom = p.Nom
                           };

                SelectList list = new SelectList(data, "roleID", "roleNom");
                ViewBag.Roles = list;


                Admin admin = AdminContext.admins.Where(x => x.adminID == id).SingleOrDefault();
                if (admin != null)
                {
                    return View("~/Views/Administration/Admin/Edit.cshtml", admin);
                }
                else
                {
                    return HttpNotFound();
                }
            }

POST:

// POST: Admin/Edit/5
        [HttpPost]
        public ActionResult Edit(Admin model)
        {
            Admin admin = AdminContext.admins.Where(x => x.adminID == model.adminID).SingleOrDefault();
            ModelState.Remove("Pass");
            if (ModelState.IsValid)
            {
                if (admin != null)
                {
                    if (model.PhotoUpload != null && model.PhotoUpload.ContentLength > 0)
                    {
                        var extension = Path.GetExtension(model.PhotoUpload.FileName);
                        var filename = model.adminID + extension;
                        var path = Path.Combine(Server.MapPath("~/Content/TemplateAdmin/assets/upload/admins"), filename);
                        model.PhotoUpload.SaveAs(path);
                        model.Photo = "/Content/TemplateAdmin/assets/upload/admins/" + filename;
                    }
                    else
                    {
                        model.Photo = admin.Photo;
                    }
                    if (model.Pass == null && model.Pass != "")
                        model.Pass = admin.Pass;

                    AdminContext.Entry(admin).CurrentValues.SetValues(model);
                    AdminContext.SaveChanges();
                }
            }
            //tempdata
            return View("~/Views/Administration/Admin/Edit.cshtml", model);
        }

修改视图:

                        <div class="form-group">
                            <label class="control-label">
                                Role <span class="symbol required"></span>
                            </label>
                            @Html.DropDownListFor(m => m.roleID, ViewBag.Roles as SelectList, "Selectioner un role", new { @class = "form-control search-select", id = "roleID" })
                        </div>

0 个答案:

没有答案