我是ASP.Net Core的新手。我试图在Createmodel
类中从Instructor
模型中填充下拉列表,并且出现了"Null reference"异常。
以下是cshtml.cs文件中的代码:
public SelectList Instructors { get; set; }
public async Task OnGetAsync()
{
var students = from s in _context.User_Profile
select s;
if (!string.IsNullOrEmpty(SearchString))
{
students = students.Where(s => s.FirstName.Contains(SearchString));
}
UserProfile = await students.ToListAsync();
var instructors = from i in _context.Instructor
orderby i.FirstName
select i;
Instructors = new SelectList(instructors, "ID", "FirstName");
}
我在cshtml视图中有以下标记:
<div class="form-group">
<label asp-for="Class_Info.Instructor" class="control-label"></label>
<select asp-for="Class_Info.Instructor" class="form-control" asp-items="@Model.Instructors">
<option value="">-- Select --</option>
</select>
</div>
我在以下位置遇到了错误:
@Model.Instructors
我遇到了这个异常 请给予建议...
var instructors
变量中是否有任何值? - marc_s@model
指令。 - Luke Vo