2016-09-09 8 views
0

私はラボ用のセッションを使用して教師推薦ウェブアプリケーションを構築しようとしていますが、特定の教師が持つ推奨事項を表示する必要がある特定のポイントに達しました。ASP.Netモデルには定義が含まれていません

app

私は勧告の番号をクリックすると、それは特定の人が持っているすべての推奨事項を示していますビューに私を取る必要がありますが、代わりに私が言ってエラーページを取得

'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>を置く誰でもすることができれば、私は私が私のビューの上部に@model IDictionary<string, Lab3Models.Models.Recommendation>を入れた場合、私は、エラーメッセージERROR

を取得し、エラーメッセージ'Person' does not contain a definition for 'Rating' and no extension method 'Rating' accepting a first argument of type 'Person' could be found

を取得を表示します私を助け、それは非常に高く評価されるだろう。

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

は、私は私のビューの上部に@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)にし、削除リンクの値に特にモデルに私のコードのエラーを取得しています。私はビューをロードすると@item.Value.Idは、私が

「KeyValuePairは」「勧告」と「KeyValuePair」

タイプの最初の引数を受け入れていない拡張メソッド「推奨」の定義が含まれていないというエラーを取得します

私は論理的にどこかに行ったのですか?

答えて

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 
    } 
} 
+0

私は、ビューを追加し、間違いなく理にかなっていることをあなたに感謝しました。だから、あなたが言っていることは、 'foreach(モデル内のvarアイテム){foreach(var rec in person.Recommendations){var rating = rec.Rating; var Narrative = rec.Narrative;等...}} ' –

+0

正しいですが、モデルではなく、直接モデルではなく、子供のオブジェクト/コレクションのメンバにアクセスする必要があります。 –

+0

今、別の性質のエラーが発生しています。それは、Recommendationコレクションを全く認識していないかのように思われます。元の投稿を編集しました。 –

関連する問題