1

I have the following code in my .cs file:

public class ClientCredentialsModel : PageModel
{
    [BindPropertyAttribute]
    public BusinessUnit businessUnit { get; set; }

    public void OnGet()
    {           
        BusinessUnit businessUnit = new BusinessUnit();

        businessUnit.Name = "Tessla";
    }
}

And

public class BusinessUnit
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string AddressName { get; set; }

    public string EmailAddress { get; set; }

    public string PostCode { get; set; }
}

..which should allow me to access the object's properties in my Razor view as:

<p>Name: @Model.businessUnit.Name</p>

However, I keep getting the following error:

Object reference not set to an instance of an object

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
  • What you need to do is decorate with [BindProperty(SupportsGet = true)] and not [BindPropertyAttribute], since you are doing a GET. – sy-huss May 13 '20 at 19:41

0 Answers0