Search code examples
c#.netasp.net-mvcasp.net-coreaspnetboilerplate

ASPNETZERO - Basic index list view error


I'm working on ASPNETZERO .NET Core MVC template. I want to setup a simple index view with tiles instead of datatables or Jtable grid. I am following the example from the docs here. I copied the code for the app service index method. I copied the view model code. I then applied the code to my index view CSHTML. As soon as I write the foreach line of code and put in model. the intellisense does not show anything. I am able to compile the code, but when I run the page, I get the error message shown below.

foreach statement cannot operate on variables of type 'CompanyIndexViewModel' because 'CompanyIndexViewModel' does not contain a public definition for 'GetEnumerator'

I need some help with this error. Also, if anyone has a code example of how I can display a simple index view with tiles instead of using a grid, please do share. I have the css/html for tiles look working. I am able to create an app service that returns a list object of my entity. I just cant seem to figure out how to get it to work on the index.cshtml page and the related index.js. I have done this many times outside of the ABP templates. I am struggling to make this work within the context of the ABP template.

Here is the view model code.

[AutoMapFrom(typeof(ListResultDto<CompanyListDto>))]
public class CompanyIndexViewModel
{
    public CompanyIndexViewModel(ListResultDto<CompanyListDto> output)
    {
        output.MapTo(this);
    }
}

Solution

  • You need to inherit ListResultDto<CompanyListDto> as shown here:

    [AutoMapFrom(typeof(ListResultDto<CompanyListDto>))]
    public class CompanyIndexViewModel : ListResultDto<CompanyListDto>
    {
        public CompanyIndexViewModel(ListResultDto<CompanyListDto> output)
        {
            output.MapTo(this);
        }
    }
    

    Then you can access Model.Items in Index.cshtml as shown here:

    @foreach (var company in Model.Items)
    {
        // ...
    }