我按照aspnet教程填充了一个选择列表,但是我遇到了NullreferenceException。本教程为:https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/update-related-data?view=aspnetcore-2.2
这是我的模特:
public class Todo
{
[Key]
public int ID { get; set; }
public Pharmacy Pharmacy { get; set; }
}
用于填充列表的模型( PharmacyNameSL 从我的数据库返回1行,没错)
public class PharmacynamePageModel : PageModel
{
public SelectList PharmacyNameSL { get; set; }
public void PopulatePharmacysDropDownList(ApplicationDbContext _context, object selectedPharmacy = null)
{
var query = (from p in _context.Pharmacy orderby p.Name select p).ToList();
PharmacyNameSL = new SelectList(query, "Pharmacy", "Name", selectedPharmacy);
}
}
Create.cshtml.cs页面
public IActionResult OnGet()
{
PopulatePharmacysDropDownList(_context);
return Page();
}
和html代码:
<div class="form-group">
<label asp-for="Todo.Pharmacy" class="control-label"></label>
<select asp-for="Todo.Pharmacy" class="form-control"
asp-items="@Model.PharmacyNameSL">
<option value="">-- Select a Pharmacy --</option>
</select>
<span asp-validation-for="Todo.Pharmacy" class="text-danger" />
</div>
当我得到/ Todos / Create时,我得到了错误:
处理请求时发生未处理的异常。 NullReferenceException:对象引用未设置为对象的实例。 Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()
希望你能帮助我;)
每笔预付款