Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
It's like I'm trying to get my menu controller to show the menus I've added in the database to see if they should be displayed or not.

but if, for example, I just get content, then I will not get the content.

my Menu.cshtml file.

@page
@model NewWebsite_Site_2018.Pages.View.ViewBar_Info.MenuModel

@foreach (var item in Model.GetListMenu)
{
    <a>@item.Name</a>
}


Menu.cshtml.cs
C#
public List<ContentInfo> GetListMenu { get; set; }


        private readonly DBContext _dbContext;

        public MenuModel(DBContext context)
        {
            _dbContext = context;
        }
        

        public void OnGet()
        {
            GetListMenu = _dbContext.ContentInfo.Where(i => i.ShowMenu == true).OrderByDescending(i => i.Id).ToList();
        }


It's my menu model file that I need to use over to my _layout.cshtml file.

_Layout.cshtml here:
@{
    var menuModel = new NewWebsite_Site_2018.Pages.View.ViewBar_Info.MenuModel();
}


@Html.Partial("View/ViewBar_Info/Menu.cshtml", menuModel.GetListMenu)




Question is so how can I get my menu to appear?

Error: There is no argument given that corresponds to the required formal parameter 'context' of 'MenuModel.MenuModel(DBContext)'

What I have tried:

private MenuModel(DBContext context)
        {
            _dbContext = context;
        }


public MenuModel(DBContext context): base()
        {
            _dbContext = context;
        }
Posted

1 solution

The error is pretty obvious:

Your MenuModel class has a single constructor, which requires a DbContext parameter:
C#
public MenuModel(DBContext context)

You are trying to create an instance of that class by calling the constructor without passing any parameters:
var menuModel = new NewWebsite_Site_2018.Pages.View.ViewBar_Info.MenuModel();

The compiler can't find a constructor which doesn't take any parameters, and so it produces the compiler error from your question.

You either need to pass the parameter to the constructor:
var menuModel = new NewWebsite_Site_2018.Pages.View.ViewBar_Info.MenuModel(new DBContext());

or add a parameterless constructor to the class:
C#
public MenuModel() : this(new DBContext()) 
{
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900