我和其他人有同样的问题我已经尝试过他们的解决方案,但没有一个能解决我的问题。 错误显示:
没有类型为'IEnumerable'的ViewData项 有关键'doctorroleID
在“提交”之后,错误显示但数据完全存储在数据库中。
这是我的观点:
<div class="form-group">
@Html.LabelFor(model => model.doctorroleID, "doctorroleID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("doctorroleID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.doctorroleID, "", new { @class = "text-danger" })
</div>
</div>
这是我的控制者:
public ActionResult Create()
{ ViewBag.doctorroleID = new SelectList(db.DoctorRoles, "doctorroleID", "doctorrole1");
return View();
}
// POST: Doctor/Create
[HttpPost]
public ActionResult Create(DoctorProfile doctor, AdminProfile admin)
{
if (ModelState.IsValid)
{
using (var db = new eoperadbEntities())
{
var crypto = new SimpleCrypto.PBKDF2();
var encrpPass = crypto.Compute(doctor.password);
var sysUser = db.DoctorProfiles.Create();
sysUser.doctorlastname = doctor.doctorlastname;
sysUser.doctorfirstname = doctor.doctorfirstname;
sysUser.doctormiddlename = doctor.doctormiddlename;
sysUser.doctorcontactnumber1 = doctor.doctorcontactnumber1;
sysUser.doctorcontactnumber2 = doctor.doctorcontactnumber2;
sysUser.doctoremail = doctor.doctoremail;
sysUser.doctorroleID = doctor.doctorroleID;
sysUser.doctorimage = doctor.doctorimage;
sysUser.username = doctor.username;
sysUser.password = encrpPass;
sysUser.passwordSalt = crypto.Salt;
sysUser.status = "Active";
db.DoctorProfiles.Add(sysUser);
db.SaveChanges();
var log = db.AccountsLogs.Create();
var doctorID = db.DoctorProfiles.Where(a => a.username == doctor.username).FirstOrDefault();
log.accountaffectedid = doctor.doctorID;
log.accountslogID = admin.adminID;
log.date = DateTime.Now;
log.operation = "Create";
db.AccountsLogs.Add(log);
db.SaveChanges();
}
ModelState.Clear();
ViewBag.Message = doctor.username + "Successfully registered!";
}
return View();
}