我的用户模型
[Key]
public int UserId { get; set; }
[Required]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required]
[DisplayName("Last Name")]
public string LastName { get; set; }
public virtual List<DptCRP> DptCRPs { get; set; }我的病人模型
[Key]
[Required]
[DisplayName("Registration No")]
public string PatientRegNo { get; set; }
[Required]
[DisplayName("Patient Name")]
public string PatientName { get; set; }
[Required]
[DisplayName("Father Name")]
public string FatherName { get; set; }
public string ReferDpt { get; set; }
public virtual List<DptCRP> DptCRPs { get; set; }My DptCRP模型(在此模型中使用UserId nad PatientRegNo作为外键)
[Key]
public int CRPId { get; set; }
[DisplayName("Provisional Diagnosis")]
public string ProvisionalDiagnosis { get; set; }
[DisplayName("Progress Report")]
public string ProgressReport { get; set; }
[Required]
public int UserId { get; set; }
public virtual User User { get; set; }
[Required]
public string PatientRegNo { get; set; }
public virtual Patient Patient { get; set; }我的部门财务主任
[HttpGet]
public ActionResult AddDptCRP()
{
var user = (from p in userdb.Users
where p.UserType == "doctor"
select p.UserId).ToList();
ViewBag.Userlist = new SelectList(user, "UserId");
var patient = (from p in patientdb.Patients
where p.ReferDpt == "dptcrp"
select p.PatientRegNo).ToList();
ViewBag.Patientlist = new SelectList(patient, "PatientRegNo");
return View();
}
[HttpPost]
public ActionResult AddDptCRP(DptCRP dptcrp)
{
try
{
if (ModelState.IsValid)
{
dptdb.DptCRPs.Add(dptcrp);
dptdb.SaveChanges();
return RedirectToAction("ListDptCRP");
}
var user = (from p in userdb.Users
where p.UserType == "doctor"
select p.UserId).ToList();
ViewBag.Userlist = new SelectList(user, "UserId");
var patient = (from p in patientdb.Patients
where p.ReferDpt == "dptcrp"
select p.PatientRegNo).ToList();
ViewBag.Patientlist = new SelectList(patient, "PatientRegNo");
return View(dptcrp);
}
catch
{
return View();
}
}当我试图在我的DptCRP中添加数据时,它会显示错误
没有具有'Userlist‘键的'IEnumerable’类型的IEnumerable项
我的AddDptCRP视图看起来就像
@model FinalYear.Models.DptCRP
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DptCRP</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProvisionalDiagnosis, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProvisionalDiagnosis, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProvisionalDiagnosis, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProgressReport, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProgressReport, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProgressReport, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserId, "UserId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Userlist", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PatientRegNo, "PatientRegNo", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Patientlist", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PatientRegNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}发布于 2015-12-17 10:08:03
下面的查询返回int的列表。
var user = (from p in userdb.Users
where p.UserType == "doctor"
select p.UserId).ToList();问题从您使用的构造函数开始。
ViewBag.Userlist = new SelectList(user, "UserId");这个构造函数
使用列表的指定项和选定的值初始化类的新实例。
与其使用此构造函数,不如使用以下构造函数:
ViewBag.Userlist = new SelectList(user, "UserId", "UserId");哪一个
使用列表的指定项、数据值字段和数据文本字段来初始化类的新实例。
在您的示例中,数据值字段和数据文本字段将是相同的。
有关SelectList的这些构造函数和其他构造函数的更多信息,请查看这里。
在视图中,您必须进行另一项更改。
这
@Html.DropDownList("Userlist", null, htmlAttributes: new { @class = "form-control"})应该写成这样:
@Html.DropDownList("Userlist", ViewBag.Customerlist as IEnumerable<SelectListItem>,htmlAttributes: new { @class = "form-control"})这里使用的构造函数如下:
public static MvcHtmlString DropDownList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> selectList,
IDictionary<string, object> htmlAttributes
)因此,如果将null作为第二个参数传递,则没有任何选择列表。有关此html助手的更多信息,请查看这里。最后但并非最不重要的是,请注意as操作符的使用。我们使用它是因为我们必须将我们的对象转换成某种类型的IEnumerable<SelectListItem>,以便将它作为参数传递给我们的html助手。ViewData是ViewDataDictionary类型,它是一个IDictionary<string, object>。这就是为什么你必须转换你的ViewBag.Userlist。
https://stackoverflow.com/questions/34330910
复制相似问题