ASP.Net模型不包含定义

本文关键字:包含 定义 模型 Net ASP | 更新日期: 2023-09-27 18:06:29

我正在尝试使用实验室会话构建一个教师推荐web应用程序,并且已经到了需要查看特定教师的推荐的特定阶段。

应用

当我点击推荐的数量时,它应该会把我带到一个列出特定人所有推荐的视图,但我会得到一个错误页面,上面写着

'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>放在视图的顶部,我会得到错误消息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)中的Model和删除链接中的Value上。@item.Value.Id当我加载视图时,我得到一个错误,说

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

我是不是在逻辑上搞错了?

ASP.Net模型不包含定义

您确实希望使用@model IDictionary,因为这是您正在使用的类型。问题是,您正在从字典中获取类型Person,并试图直接显示该类型的评级。如果没有看到你的前端代码,我无法准确地指出问题是如何出现的,但可以告诉你问题是什么。本质上,你试图从person对象获取Rating属性,但Rating属性是person对象的推荐集合的一部分。

我在这里假设您正在遍历字典中的每个Person来构建显示。如果你想访问评分,你还需要迭代每个人的每个推荐。

大致

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