exlnt
exlnt

Reputation: 293

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

Upvotes: 0

Views: 319

Answers (2)

aaron
aaron

Reputation: 43083

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)
{
    // ...
}

Upvotes: 2

exlnt
exlnt

Reputation: 293

I figured out a solution to my issue for now.

I updated my view model with below code.

[AutoMapFrom(typeof(ListResultDto<CompanyListDto>))]
public class CompanyIndexViewModel
{
    public IReadOnlyList<CompanyListDto> Companies { get; }

    public CompanyIndexViewModel(IReadOnlyList<CompanyListDto> companies)
    {
        Companies = companies;
    }
}

I then updated my controller code to below.

 var output = _companyAppService.GetCompanyList(input);
 var model = new CompanyIndexViewModel(output.Items);
 return View(model);

I got this solution from reading this article here

Upvotes: 0

Related Questions