user2906420
user2906420

Reputation: 1269

MVC Razor The model item passed into the dictionary is of type List String, but this dictionary requires IEnumerable

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.

Upvotes: 1

Views: 1957

Answers (2)

MRB
MRB

Reputation: 3812

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

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125488

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

Upvotes: 1

Related Questions