无法将模型值分配给剃须刀页面中的参数

Not able to assign model value to a parameter in razor pages

我有一个用于显示页面的剃刀页面代码。

public class RegisterModel : PageModel
{

    [BindProperty]
    public InputModel Input { get; set; }

    public string Test {get; set;}
    public class InputModel
    {        
        public List<SelectListItem> QList { get; set; }
    }

    public void OnGet()
    {
        Helper helper = new Helper(_logger, _context);
        var Test = helper.GetTestString();
        var saltyList = helper.GetAllApples();

        Input.QList = saltyList;
    }
}

变量 saltyList 有 15 个值。但是我收到一个错误:

{System.ExecutionEngineException: Exception of type 'System.ExecutionEngineException' was thrown.}

我错过了什么吗?是不是无法在 Get 方法中赋值,还是我遗漏了什么?我在赋值之前没有初始化 Input.Qlist 是否有问题?测试工作正常,但 InputModel 分配不正常。

您需要在OnGet方法中为Input赋值。像这样:

public void OnGet()
{
    Helper helper = new Helper(_logger, _context);
    var Test = helper.GetTestString();
    var saltyList = helper.GetAllApples();

    Input = new InputModel
            {   
                QuestionList = questionList
            };
}

如果您想像现在这样绑定 Get 中的值,您可能必须对 Bindproperty 使用 SupportGet= true。

[BindProperty(SupportGet=true)]
public InputModel Input { get; set; }

您可以阅读更多相关信息here