我已使用Contoso University教程作为我的项目的基础-我已经进行了四次检查,以确保没有错别字错误(事实可以编译)
但是当我单击“新建”时,出现上述错误。
断点
RentalPropertyNameSL = new
SelectList(propertyQuery.AsNoTracking(),
"RentalPropertyID",
"PropertyName", selectedRentalProperty);
在F10之后,RentalPropertyNameSL为空
Name Value Type
▶ RentalPropertyNameSL
{Microsoft.AspNetCore.Mvc.Rendering.SelectList} Microsoft.AspNetCore.Mvc.Rendering.SelectList
显示正确,但引发此错误
NullReferenceException: Object reference
not set to an instance of an object.
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()
Stack Query Cookies Headers
NullReferenceException: Object reference
not set to an instance of an object.
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItemsWithValueField()
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetListItems()
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.GetEnumerator()
System.Collections.Generic.List<T>.AddEnumerable(IEnumerable<T> enumerable)
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateGroupsAndOptions(string optionLabel, IEnumerable<SelectListItem> selectList, ICollection<string> currentValues)
Microsoft.AspNetCore.Mvc.ViewFeatures.DefaultHtmlGenerator.GenerateSelect(ViewContext viewContext, ModelExplorer modelExplorer, string optionLabel, string expression, IEnumerable<SelectListItem> selectList, ICollection<string> currentValues, bool allowMultiple, object htmlAttributes)
Microsoft.AspNetCore.Mvc.TagHelpers.SelectTagHelper.Process(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
Rent2.Pages.Tenants.Pages_Tenants_Create.<ExecuteAsync>b__21_0() in Create.cshtml
<button class="expandCollapseButton" data-frameid="frame11" style="cursor:pointer;float:left;height:16px;width:16px;font-size:10px;background-color:#eeeeee;padding:0px;border-width:0px;border-style:initial;border-color:initial;margin:0px;">+</button>
asp-items="@Model.RentalPropertyNameSL">
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)
Rent2.Pages.Tenants.Pages_Tenants_Create.ExecuteAsync() in Create.cshtml
<button class="expandCollapseButton" data-frameid="frame15" style="cursor:pointer;float:left;height:16px;width:16px;font-size:10px;background-color:#eeeeee;padding:0px;border-width:0px;border-style:initial;border-color:initial;margin:0px;">+</button>
ViewData["Title"] = "Create Course";
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
using Rent2.Data;
using System.Linq;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace Rent2.Pages.Tenants
{
public class RentalPropertyNamePageModel : PageModel
{
public SelectList RentalPropertyNameSL { get; set; }
public void PopulateRentalPropertyDropDownList(RentContext _context,
object selectedRentalProperty = null)
{
var propertyQuery = from d in _context.RentalProperties
orderby d.PropertyName
select d;
RentalPropertyNameSL = new SelectList(propertyQuery.AsNoTracking(),
"RentalPropertyID", "PropertyName", selectedRentalProperty);
}
}
}
@page
@model Rent2.Pages.Tenants.CreateModel
@{
ViewData["Title"] = "Create Course";
}
<h2>Create</h2>
<h4>Tenant</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Tenant.LastName" class="control-label"></label>
<input asp-for="Tenant.LastName" class="form-control" />
<span asp-validation-for="Tenant.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tenant.FirstMidName" class="control-label"></label>
<input asp-for="Tenant.FirstMidName" class="form-control" />
<span asp-validation-for="Tenant.FirstMidName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tenant.StartDate" class="control-label"></label>
<input asp-for="Tenant.StartDate" class="form-control" />
<span asp-validation-for="Tenant.StartDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tenant.VacatingDate" class="control-label"></label>
<input asp-for="Tenant.VacatingDate" class="form-control" />
<span asp-validation-for="Tenant.VacatingDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tenant.MobileNumber" class="control-label"></label>
<input asp-for="Tenant.MobileNumber" class="form-control" />
<span asp-validation-for="Tenant.MobileNumber" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tenant.RentalProperty" class="control-label"></label>
<select asp-for="Tenant.RentalPropertyID" class="form-control"
asp-items="@Model.RentalPropertyNameSL">
<option value="">-- Select Rental Property --</option>
</select>
<span asp-validation-for="Tenant.RentalPropertyID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Here's Contoso's two code pages
using ContosoUniversity.Data;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace ContosoUniversity.Pages.Courses
{
public class DepartmentNamePageModel : PageModel
{
public SelectList DepartmentNameSL { get; set; }
public void PopulateDepartmentsDropDownList(SchoolContext _context,
object selectedDepartment = null)
{
var departmentsQuery = from d in _context.Departments
orderby d.Name // Sort by name.
select d;
DepartmentNameSL = new SelectList(departmentsQuery.AsNoTracking(),
"DepartmentID", "Name", selectedDepartment);
}
}
}
@page
@model ContosoUniversity.Pages.Courses.CreateModel
@{
ViewData["Title"] = "Create Course";
}
<h2>Create</h2>
<h4>Course</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Course.CourseID" class="control-label"></label>
<input asp-for="Course.CourseID" class="form-control" />
<span asp-validation-for="Course.CourseID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Course.Title" class="control-label"></label>
<input asp-for="Course.Title" class="form-control" />
<span asp-validation-for="Course.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Course.Credits" class="control-label"></label>
<input asp-for="Course.Credits" class="form-control" />
<span asp-validation-for="Course.Credits" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Course.Department" class="control-label"></label>
<select asp-for="Course.DepartmentID" class="form-control"
asp-items="@Model.DepartmentNameSL">
<option value="">-- Select Department --</option>
</select>
<span asp-validation-for="Course.DepartmentID" class="text-danger" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
enter code here