[英]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.