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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x