我正在尝试按照此处的示例代码进行操作,但我一定遗漏了一些明显的内容。我没有从数据源中读取我的选择列表选项,而是尝试在构造函数中加载它们。但我不断收到错误消息。
InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Mvc.Rendering.SelectList'. Model bound complex types must not be abstract, or value types and must have a parameterless constructor. Alternatively, set the 'SearchOptions' property to a non-null value.
Run Code Online (Sandbox Code Playgroud)
这是CS代码:
public class TestSelectModel : PageModel
{
private List<SelectListItem> _searchoptions;
[BindProperty(SupportsGet = true)]
public SelectList SearchOptions { get; set; }
public TestSelectModel()
{
_searchoptions = new List<SelectListItem>();
_searchoptions.Add(new SelectListItem("By Email", "By Email", true));
_searchoptions.Add(new SelectListItem("By Request Name", "By Request Name", false));
}
public void OnGet()
{
SearchOptions = new SelectList(_searchoptions, "Key", "Value", "By Email");
}
}
Run Code Online (Sandbox Code Playgroud)
这是 cshtml 代码:
<h2>TestSelect</h2>
<form>
<div>
<select asp-items="Model.SearchOptions">
<option value="">Choose an search method</option>
</select>
<input type="submit" value="Filter" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,这很容易,但我就是看不到它。
编辑
每个评论还尝试了下面的代码,它确实运行了 OnGet() 函数,但它抛出NullReferenceException: Object reference not set to an instance of an object
.
private List<SelectListItem> _searchoptions;
public SelectList SearchOptions { get; set; }
[BindProperty(SupportsGet = true)]
public string BoundSearchField { get; set; }
public TestSelectModel()
{
_searchoptions = new List<SelectListItem>();
_searchoptions.Add(new SelectListItem("By Email", "By Email", true));
_searchoptions.Add(new SelectListItem("By Request Name", "By Request Name", false));
}
public void OnGet()
{
SearchOptions = new SelectList(_searchoptions, "Key", "Value", "By Email");
}
<form>
<div>
<select name="BoundSearchField" asp-items="Model.SearchOptions">
<option value="">Choose an search method</option>
</select>
<input type="submit" value="Filter" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
原始问题的答案
为什么我收到这个错误
InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Mvc.Rendering.SelectList'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the 'SearchOptions' property to a non-null value
BindProperty
在您的属性上具有该属性SearchOptions
将尝试将标头数据绑定到您的SelectList
. 从您的选择列表中删除 BindPropery 属性并创建一个属性来接收所选值。
您编辑中问题的答案
现在我收到错误
NullReferenceException: Object reference not set to an instance of an object
是因为"Key"
应该是"Text"
更改
SearchOptions = new SelectList(_searchoptions, "Key", "Value", "By Email");
为SearchOptions = new SelectList(_searchoptions, "Text", "Value", "By Email");
完整代码:
private List<SelectListItem> _searchoptions;
// [BindProperty(SupportsGet = true)] // Remove this here
public SelectList SearchOptions { get; set; }
// Add a property to receive your selected value
[BindProperty(SupportsGet = true)]
public string BoundSearchField { get; set; }
public TestSelectModel()
{
_searchoptions = new List<SelectListItem>();
_searchoptions.Add(new SelectListItem("By Email", "By Email", true));
_searchoptions.Add(new SelectListItem("By Request Name", "By Request Name", false));
}
public void OnGet()
{
// Change "Key" to "Text"
SearchOptions = new SelectList(_searchoptions, "Text", "Value", "By Email");
}
Run Code Online (Sandbox Code Playgroud)
然后添加name="BoundSearchField"
到您的 .cshtml
<form>
<div>
<select name="BoundSearchField" asp-items="Model.SearchOptions">
<option value="">Choose an search method</option>
</select>
<input type="submit" value="Filter" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)