【发布时间】:2019-03-05 05:50:29
【问题描述】:
我遇到了一些我似乎无法使用 RazorPages 并从 SQL DB 填充 SelectList 的问题。我遵循了一个关于将数据绑定到选择列表的教程,现在当我发布我的表单时,它会出现以下错误:
InvalidOperationException:无法创建“Microsoft.AspNetCore.Mvc.Rendering.SelectList”类型的实例。模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数。或者,在“TestProject.Pages.IndexModel”构造函数中将“CarMakeList”属性设置为非空值。
我有以下代码:
public class IndexModel : PageModel
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IVehicleService _vehicleService;
private readonly ApplicationDbContext _context;
public IndexModel(IHostingEnvironment hostingEnvironment, IVehicleService vehicleService, ApplicationDbContext context)
{
_hostingEnvironment = hostingEnvironment;
_vehicleService = vehicleService;
_context = context;
}
public Filters Filters { get; set; }
public SelectList CarMakeList { get; set; }
public IActionResult OnGet()
{
PopulateMakeDropdownList();
return Page();
}
[BindProperty]
public IFormFile Upload { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var file = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", Upload.FileName);
using (var fileStream = new FileStream(file, FileMode.Create))
{
await Upload.CopyToAsync(fileStream);
}
HttpContext.Session.SetObject("CurrentFilters", Filters);
return RedirectToPage("vehicles");
}
public void PopulateMakeDropdownList(object selectedMake = null)
{
var makeQuery = from m in _context.CarMake
orderby m.Name
select m;
CarMakeList = new SelectList(makeQuery.AsNoTracking(), "MakeId", "Name", selectedMake);
}
public class CarMake
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("MakeId")]
public int MakeId { get; set; }
public string Name { get; set; }
}
这是我的过滤器类
public class Filters
{
[Required]
public int Source { get; set; }
public int MmrPercentage { get; set; }
public int MaximumPrice { get; set; }
public int MinimumPrice { get; set; }
public int Transport { get; set; }
public int Recon { get; set; }
public int ExtraCost { get; set; }
public int MinimumOdometer { get; set; }
public int MaximumOdometer { get; set; }
public string InitialSortOption { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}
我的前端是这样的:
<div class="row">
<div class="form-group">
<label asp-for="Filters.Make">Vehicle Make:</label>
<select id="makeDropDownList" asp-for="Filters.Make" class="form-control"
asp-items="@Model.CarMakeList">
<option value="-1">-- Select Make --</option>
</select>
</div>
</div>
我已经搞砸了大约一个小时,不知道为什么会这样。在 await next.Invoke() 上的这段代码中,它在我的 startup.cs 文件中死掉了
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.Response.StatusCode == StatusCodes.Status404NotFound)
{
context.Response.Redirect("/AndItsGone");
}
});
编辑:在我将 Make/Model 选择列表添加到解决方案之前,一切都运行良好。页面加载正常,只要我点击提交按钮,我就会收到错误消息。
【问题讨论】:
-
你能给我们提供方法 PopulateMakeDropdownList 所在的类的完整代码吗?
-
Filters.Make的定义是什么? -
Model bound complex types must not be abstract or value types and must have a parameterless constructor.。可以添加CarMake描述吗? -
我有同样的问题,无法解决。看来您在绑定 SelectList 时遇到问题,而我的问题是绑定 PagedList。该错误表明您可以在构造函数中将属性设置为非空,但它不起作用。 stackoverflow.com/questions/55926657/…
标签: c# asp.net-core razor-pages