1
votes

Model:

public class TestModel
{
    [Column]
    public string Name { get; set; }
}

Controller:

TestModel[] metadata = context.GetTable<TestModel>().ToArray();


return View(itemsToShow.ToList());

the name column and sorts it alphabethically on user selection which works fine (have tested it in debug mode - for example if user selects 'd' it only will display the items starting with 'd'.)

The problem is when i add "@model IEnumerable" and foreach loop in the view I get the error.

2
Your items collection is of type List<string>, not List<TestModel>.Jeroen Vannevel

2 Answers

1
votes

The problem is your view is expecting a type of IEnumerable<TestModel> for the model but you are passing in IList<String> as the error message indicates.

It looks like you shouldn't be projecting metadata to an IList<String> in your controller and passing to the view but instead should be filtering metadata like

IEnumerable<TestModel> metadata = context.GetTable<TestModel>().ToArray();

var itemsToShow = string.IsNullOrEmpty (page) 
    ? metadata 
    : metadata.Where(x => x.Name.StartsWith(page, true, null));

ViewData["currentPage"] = page ?? string.Empty;

return View(itemsToShow.ToList());
0
votes

Why you call .ToList() on your itemsToShow ? (i think it is only a duplication)

Change List<string> items to IEnumerable<string> items and remove .ToList() from last line