我有以下实体:
Not Authorized
我有这个控制器动作:
public partial class Enrollments
{
[Key]
public int ID { get; set; }
public int RegistrationID { get; set; }
public int ModuleID { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Enrollment Date")]
public Nullable<System.DateTime> EnrollmentDate { get; set; }
[Display(Name = "Price Agreed")]
public Nullable<decimal> PriceAgreed { get; set; }
public Nullable<decimal> Grade { get; set; }
[NotMapped]
public virtual Modules Modules { get; set; }
[NotMapped]
public virtual Registrations Registrations { get; set; }
[NotMapped]
[Display(Name = "Student")]
public virtual String StudentName { get; set; }
[NotMapped]
public virtual int StudentID { get; set; }
[NotMapped]
[Display(Name = "Course")]
public virtual String CourseName { get; set; }
[NotMapped]
public virtual int CourseID { get; set; }
[NotMapped]
[Display(Name = "Module")]
public virtual String ModuleName { get; set; }
}
我认为:
public ActionResult Create()
{
ViewBag.StudentNames = new SelectList(db.Students.Select(x =>
new SelectListItem
{
Value = x.ID.ToString(),
Text = x.LastName.Trim() + ", " + x.FirstName
}),
"StudentID",
"StudentName");
return View();
}
我收到此错误:DataBinding:'System.Web.Mvc.SelectListItem'不包含名称为'StudentID'的属性。
我检查了许多其他类似的问题,实际上,此代码基于其中一个答案,但仍然出现错误。
答案 0 :(得分:1)
您收到此错误,因为SelectListItem确实没有属性StudentyID。 将初始化更改为
ViewBag.StudentNames = new SelectList(db.Students.Select(x =>
new SelectListItem
{
Value = x.ID.ToString(),
Text = x.StudentName
}),
"Value",
"Text");
或类似的东西
ViewBag.StudentNames = new SelectList(db.Students.Select(x =>
new
{
StudentID = x.ID.ToString(),
StudentName = x.StudentName
}),
"StudentID",
"StudentName");