简体   繁体   English

ASP.Net模型不包含定义

[英]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 当我将@model IDictionary<string, Lab3Models.Models.Person>放到我的表演的顶部时,我收到错误消息'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 如果将@model IDictionary<string, Lab3Models.Models.Recommendation>放在我的视图顶部@model IDictionary<string, Lab3Models.Models.Recommendation>收到错误消息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 编辑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: 我在视图顶部有@model IDictionary<string, Lab3Models.Models.Recommendation> ,并已将视图中的代码更改为如下所示:

 @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. 但是我在代码中特别在此语句@foreach (var item in Model)模型中的@foreach (var item in Model)和删除链接中的Value上出现错误。 @item.Value.Id When I load the view, I get an error saying @item.Value.Id加载视图时,出现错误提示

'KeyValuePair' does not contain a definition for 'Recommendations' and no extension method 'Recommendations' accepting a first argument of type 'KeyValuePair' 'KeyValuePair'不包含'Recommendations'的定义,也没有扩展方法'Recommendations'接受类型为'KeyValuePair'的第一个参数

Did I goof up somewhere logically? 我在逻辑上犯错了吗?

You do want to use @model IDictionary, as that's the type you are using. 您确实要使用@model IDictionary,因为这就是您使用的类型。 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. 本质上,您试图从person对象获取Rating属性,但是Rating属性是Person对象的Recommendation Collection的一部分。

I'm assuming here that you are iterating through each Person in the dictionary to build out the display. 我在这里假设您要遍历字典中的每个Person来构建显示。 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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