简体   繁体   中英

ASP.Net Model does not contain definition

I'm trying to build a teacher recommendation web app using sessions for lab, and have gotten to a particular point where I need to view the recommendations that a particular teacher has.

app

When I click on the number of recommendations, it should take me to a view that lists all the recommendations that particular person has, but instead I get an error page saying

'Lab3Models.Models.Person' does not contain a definition for 'Rating'

Here's some of my code, hopefully someone can point me in the right direction.

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 Add(string id) { if (Session["people"] != null) { _people = (Dictionary<string, Person>)Session["people"]; } else { _people = new Dictionary<string, Person>(); Session["people"] = _people; } return View(_people[id]); } [HttpPost] public ActionResult Create(string personId, Recommendation recommendation) { if (personId == null) { return HttpNotFound("Error, ID not found"); } else { _people[personId].Recommendations.Add(recommendation); return RedirectToAction("Index", "Home"); } } public ActionResult Show(string id) { if (Session["people"] != null) { _people = (Dictionary<string, Person>)Session["people"]; } else { _people = new Dictionary<string, Person>(); Session["people"] = _people; } return View(_people); } } } 

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 int NumberOfRecommendations { get { return Recommendations.Count; } } 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; } } } 

When I put @model IDictionary<string, Lab3Models.Models.Person> in the top of my Show I get the error message 'Person' does not contain a definition for 'Rating' and no extension method 'Rating' accepting a first argument of type 'Person' could be found

If I put @model IDictionary<string, Lab3Models.Models.Recommendation> in the top of my view I get the error message ERROR

If anyone could help me out, it'd be greatly appreciated.

EDIT

 @model IDictionary<string, Lab3Models.Models.Recommendation> @{ ViewBag.Title = "Show"; } <h2>Show</h2> <table class="table"> <tr> <th> ... </th> </tr> @foreach (var item in Model) { <tr> <td> @item.Value.Id </td> <td> @item.Value.Rating </td> <td> @item.Value.Narrative </td> <td> @item.Value.RecommenderName </td> <td> <a href="/recommendation/delete/@item.Value.Id">Delete</a> | </td> </tr> } </table> 

EDIT 2

I have @model IDictionary<string, Lab3Models.Models.Recommendation> at the top of my view and have changed the code in my view to look like this:

 @foreach (var item in Model) { foreach (var rec in item.Recommendations) { var rating = rec.Rating; var narr = rec.Narrative; ... <tr> <td>@rating</td> <td>@narr</td> <td>@recName</td> <td> <a href="/recommendation/delete/@item.Value.Id">Delete</a> </td> </tr> } } 

But I'm getting errors in my code specifically on Model in this statement @foreach (var item in Model) and on Value in the delete link. @item.Value.Id When I load the view, I get an error saying

'KeyValuePair' does not contain a definition for 'Recommendations' and no extension method 'Recommendations' accepting a first argument of type 'KeyValuePair'

Did I goof up somewhere logically?

You do want to use @model IDictionary, as that's the type you are using. The issue is that you are getting a type Person out of the dictionary, and attempting to display rating directly from that type. Without seeing your front-end code I can't pinpoint exactly how the issue is presenting, but can tell you what your issue is. Essentially, you are attempting to get the Rating property from the person object, but the Rating property is part of the Person object's Recommendation Collection.

I'm assuming here that you are iterating through each Person in the dictionary to build out the display. You also need to iterate through each Recommendation for each person if you want to access the Rating.

roughly

foreach(var person in @model) {
    //person specific display things
    foreach(var recommendation in person.Recommendations) {
        var rating = recommendation.Rating;
        // display rating things
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM