3

I've created a web app where you can leave recommendations for a person, a person can have multiple recommendations, and got to the point where I'd like to view all recommendations for a particular Person. Once I click on the link to view the recommendations I get an error saying :

'KeyValuePair <`string, Recommendation> does not contain a definition for 'Recommendations' and no extension method 'Recommendations' accepting a first argument of type 'KeyValuePair' could be found.

Here's some of my code, hopefully someone can help me resolve this issue I've been stuck on for hours.

Recommendation Controller

using Lab3Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lab3Models.Controllers
{
    public class RecommendationController : Controller
    {
        private static IDictionary<string, Person> _people = null;
        
        ....

        public ActionResult Show(string id)
        {
            if (Session["people"] != null)
            {
                _people = (Dictionary<string, Person>)Session["people"];
            }
            else
            {
              _people = new Dictionary<string, Person>();
                Session["people"] = _people;
            }
            if (!_people.ContainsKey(id))
            {
                return HttpNotFound("Sorry, but can't find id: " + id);
            }
            return View(_people[id]);    
        }
    }
}
Person & Recommendation Models

public class Person
    {
        public string Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public ICollection<Recommendation> Recommendations { get; set; }

        public Person()
        {
            Recommendations = new List<Recommendation>();
        }
    }


public class Recommendation
    { 
        public string Id { get; set; }
        public int Rating { get; set; }
        public string Narrative { get; set; }
        public string RecommenderName { get; set; }
        public Person ThePerson { get; set; }
    }

Show View

@model IDictionary<string, Lab3Models.Models.Recommendation>

<h2>@ViewBag.Title</h2>

<table class="table">
    <tr>
        <th>
            Id
        </th>
        <th>
            Rating
        </th>
        <th>
            Narrative
        </th>
        <th>
            Recommender's Name
        </th>
    </tr>
             
    @foreach (var item in Model)
    {
        foreach (var rec in item.Recommendations)
        {
            <tr>
                <td>
                    @item.Value.Id
                </td>
                <td>
                    @item.Value.Rating
                </td>
                <td>
                    @item.Value.Narrative
                </td>
                <td>
                    @item.Value.RecommenderName
                </td>
            </tr>
        }
    }
</table>

Please, any help would be great.

EDIT Iterating over a dictionary isn't my problem though, I've iterated through my person dictionary already to display all people I've added to the list. My problem is showing the list of recommendations for a particular person from the original person dictionary.

  • Your model in the view is `IDictionary` and your using a nested `foreach` loop to attempt to iterate over it which is not correct (it is a problem and you need to refer the dupe to understand how to iterate over a dictionary). But your view makes no sense and has no relationship to the controller code you have show which returns `Person` to a view (not a `Dictionary`) and then it would be `foreach(var rec in Model.Recommendations) { ... }` –  Sep 10 '16 at 12:27
  • So, essentially my Show method in the controller is wrong because theres no relationship to recommendation? –  Sep 10 '16 at 12:35
  • `return View(_people[id]);` returns a `Person` which contains property a collection of `Recommendation`, so your view would need to be `@model Person` (but you cannot have shown the correct code since this would throw a different exception) –  Sep 10 '16 at 12:42

0 Answers0