我正在尝试使用实验室会话构建教师推荐网络应用程序,并且已经达到了我需要查看特定教师所具有的建议的特定点。
当我点击建议的数量时,它应该带我到一个视图,列出特定人员的所有建议,但我得到一个错误页面说
'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);
}
}
}
&#13;
人与人推荐模型
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; }
}
}
&#13;
当我将@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>
&#13;
编辑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>
}
}
&#13;
但是我在我的代码中特意在此语句@foreach (var item in Model)
中的模型和删除链接中的值上获得了错误。 @item.Value.Id
当我加载视图时,出现错误
&#39; KeyValuePair&#39;不包含&#39;建议&#39;的定义并没有推广方法&#39;建议&#39;接受类型&#39; KeyValuePair&#39;
的第一个参数
我是否逻辑地在某个地方搞砸了?
答案 0 :(得分:0)
您确实想要使用@model IDictionary,因为这是您正在使用的类型。问题是您从字典中获取类型Person,并尝试直接从该类型显示评级。如果没有看到您的前端代码,我无法确切地指出问题的确切呈现方式,但可以告诉您问题所在。从本质上讲,您试图从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
}
}