Search code examples
c#asp.net-coreasp.net-core-2.1razor-pages

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


I've got a razor page code which I use to display a page.

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;
    }
}

The variable saltyList has 15 values. But I am getting an error :

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

Am I missing something? Is it not possible to assign value in Get method or am I missing something?Is there an issue in terms of me not having initialized Input.Qlist to anything before assigning the value? Test works fine but not the InputModel assignment.


Solution

  • You need to assign the value of Input in the OnGet method. Something like this :

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

    If you want to bind the value in Get like how you are doing, you might have to use SupportGet= true for Bindproperty.

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

    You can read more about it here