user1361914
user1361914

Reputation: 411

Passing dictionary to a View

I am passing this type into my view

Dictionary<String, List<myObj>> results = ...

View(results);

My view has this declaration

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Models.myObj>>" %>

I run into this error:

`The model item passed into the dictionary is of type 'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[Models.myObj]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Models.myObj]'.`

This is because of the new structure I am sending down to the view. How do I auto update the view so it uses the new param type?

I am not using Razor view

The view will display accordion, with the key being the name of accorion and the value oof the dictionary will be the data displayed when accordion tab is expanded

Upvotes: 0

Views: 288

Answers (1)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

Change the type in your view too:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IDictionary<string, List<Models.myObj>>>" %>

I'm a bit rusty with the ASPX view engine but you should be able to loop like this:

<% foreach(var item in Model) { %>

   //some html

<% } %>

Upvotes: 1

Related Questions