Henry Zhu
Henry Zhu

Reputation: 2628

Azure .NET MVC Core Application: A suitable constructor for Model type could not be located

I am trying to use a model I created (RoleAssignmentRecord) in my Index.cshtml file (in the Pages folder). Below is my Index.cshtml code:

@page
@using AccessChangeMonitoringUI.Models
@model RoleAssignmentRecord

<div class="text-center">
    <h1 class="display-4">OneAuthZ Change Monitoring</h1>
    <p>View OneAuthZ role change history and acknowledge role changes.</p>
    @using (Html.BeginForm("RoleAssignmentController", "TestForm", FormMethod.Get))
    {
        <label>Test Form</label>
        <input type="submit" name="submit" value="Submit" />
    }
</div>

Below is my RoleAssignmentRecord code in the Models folder:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AccessChangeMonitoringUI.Models
{
    public class RoleAssignmentRecord
    {
        [JsonProperty(PropertyName = "id")]
        string id { get; set; }
        [JsonProperty(PropertyName = "RoleAssignmentId")]
        string roleId { get; set; }
        List<RoleAssignmentChange> changeHistory { get; set; }
        RoleAssignment currentRow { get; set; }

        RoleAssignmentRecord(RoleAssignment currentRow, List<RoleAssignmentChange> changeHistory)
        {
            this.id = currentRow.Id;
            this.roleId = currentRow.Id;
            this.currentRow = currentRow;
            this.changeHistory = changeHistory;
        }
    }
}

However, when I run my application, I receive the following error:

InvalidOperationException: A suitable constructor for type 'AccessChangeMonitoringUI.Models.RoleAssignmentRecord' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

This seems strange, considering that I do have a constructor for RoleAssignmentRecord

Upvotes: 0

Views: 624

Answers (1)

Joey Cai
Joey Cai

Reputation: 20127

Note that if you do not use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

It should be public otherwise the DI cannot access it:

public RoleAssignmentRecord(RoleAssignment currentRow, List<RoleAssignmentChange> changeHistory)
{
    this.id = currentRow.Id;
    this.roleId = currentRow.Id;
    this.currentRow = currentRow;
    this.changeHistory = changeHistory;
}

Upvotes: 1

Related Questions