【问题标题】:Not able to assign model value to a parameter in razor pages无法将模型值分配给剃须刀页面中的参数
【发布时间】:2019-05-09 14:42:11
【问题描述】:

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

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:引发了“System.ExecutionEngineException”类型的异常。}

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

【问题讨论】:

    标签: c# asp.net-core asp.net-core-2.1 razor-pages


    【解决方案1】:

    您需要在 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

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2018-05-30
      • 2018-09-18
      • 2020-11-02
      相关资源
      最近更新 更多