繁体   English   中英

ASP.Net模型不包含定义

[英]ASP.Net Model does not contain definition

我正在尝试使用实验室课程来构建教师推荐网络应用程序,并且已经达到了需要查看特定教师提出的推荐的特定点。

应用程式

当我单击建议数量时,应该带我到一个列出特定人员拥有的所有建议的视图,但是我看到一个错误页面,提示

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

这是我的一些代码,希望有人可以指出正确的方向。

推荐负责人

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

人员和推荐模型

  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; } } } 

当我将@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

如果将@model IDictionary<string, Lab3Models.Models.Recommendation>放在我的视图顶部@model IDictionary<string, Lab3Models.Models.Recommendation>收到错误消息ERROR

如果有人可以帮助我,将不胜感激。

编辑

 @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> 

编辑2

我在视图顶部有@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> } } 

但是我在代码中特别在此语句@foreach (var item in Model)模型中的@foreach (var item in Model)和删除链接中的Value上出现错误。 @item.Value.Id加载视图时,出现错误提示

'KeyValuePair'不包含'Recommendations'的定义,也没有扩展方法'Recommendations'接受类型为'KeyValuePair'的第一个参数

我在逻辑上犯错了吗?

您确实要使用@model IDictionary,因为这就是您使用的类型。 问题是您从词典中找到了类型“人”,并试图直接从该类型显示评级。 在没有看到您的前端代码的情况下,我无法确切指出问题的表现方式,但可以告诉您问题是什么。 本质上,您试图从person对象获取Rating属性,但是Rating属性是Person对象的Recommendation Collection的一部分。

我在这里假设您要遍历字典中的每个Person来构建显示。 如果要访问评级,还需要遍历每个人的每个建议。

大致

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