Razor页面上的SelectList模型绑定错误

时间:2019-03-02 01:04:51

标签: c# razor-pages

我正在尝试遵循示例代码here,但是我必须缺少明显的东西。与其从数据源中读取我的选择列表选项,我只是尝试将它们加载到构造函数中。但我不断收到错误消息

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

这是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");

    }
}

这是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>

我觉得这很容易,但我看不到。

编辑

每个注释还尝试了下面的代码,该代码确实运行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>

2 个答案:

答案 0 :(得分:0)

原始问题的答案

  

我为什么收到此错误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");
}

然后将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>

答案 1 :(得分:0)

问题似乎在于如何定义retrieveFoo { result in switch result { case .failure(NetworkRequestError.api(_, .recordNotFound, _)): // handle specific “record not found” error here case .failure(let error): // handle all other errors here case .success(let foo): // do something with `foo` } } SelectList。在调试模式下,它们看起来不错,但我认为问题出在渲染尝试发生时。这是起作用的。请注意,所做的更改是我为项目添加了SelectListItemsText的定义,然后在加载列表时将其复制了。另请注意,发布后,我必须使用绑定字段来保存用户选择。我真的很好奇Razor example that I was referencing不需要它。

Value