キー "roleID"を持つViewDataアイテムは "System.Int32"ですが、 「IEnumerable」と入力します。キー "KEY"を持つViewDataアイテムは "System.Int32"ですが、タイプが 'IEnumerable <SelectListItem>'である必要があります
私はそれを修正できません 私はあなたの助けが必要です!これは私のコードです:
管理モデル:
[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>
コントローラであなたのPOSTアクションとは何ですか? – Taosique
私は答えとして入れます –