2016-10-30 5 views
1

私は、異なるグループの人々のリストを含むPersonListクラスを作成しました。私のコントローラでは、そのクラスをビューに渡します。私のビューは強く型付けされているので、私はそのクラスを参照しています。しかし私の見解では、そのグループのタイプ(学生、退職者、政治家)が必要なので、ビューの条件文でそれらを使用できます。私はPersonListクラスでグループごとに1つのオブジェクトを追加することでこの問題を解決しましたが、それを行うには最良の方法ではないと感じています。より良い解決策を私に指示してください。MVCの@modelスコープ外で宣言された型へのアクセス

.cs file

public interface IPerson 
{ 
} 
public abstract class Person 
{ 
    public string Name { get; set; } 
    public string LastName { get; set; } 
    public Int16 Age { get; set; } 
} 
public class Politics : Person, IPerson 
{ 
    public string Party { get; set; } 
    public int Experience { get; set; } 
} 
public class Students : Person, IPerson 
{ 
    public string Major { get; set; } 
    public int Year { get; set; } 
} 
public class Retired: Person, IPerson 
{ 
    public int Pension { get; set; } 
    public string Hobby { get; set; } 
} 
public class PersonLists : IPerson 
{ 
    public List<Retired> AllRetired = new List<Retired>(); 
    public List<IPerson> AllPeople = new List<IPerson>(); 
    public Students AStudent = new Students(); 
    public Politics APolitic = new Politics(); 
    public Retired ARetired = new Retired(); 
} 

cshtml file:

@model ConferenceAttendees.Models.PersonLists 
<style> 
h6 { 
    display: inline 
} 
th{ 
    text-align:center; 

} 
</style> 
<div> 
    <table style="width:100%" align="left"> 
     <tr> 
      <th>Imie</th> 
      <th>Nazwisko</th> 
      <th>Wiek</th> 
      <th>Partia</th> 
      <th>Doswiadczenie</th> 
      <th>Kierunek</th> 
      <th>Rok</th> 
     </tr> 
      @foreach (dynamic item in Model.AllPeople) 
      { 
       <tr> 
       <th>@item.Name</th> 
       <th>@item.LastName</th> 
       <th>@item.Age</th> 
       @if (item.GetType() == Model.APolitic.GetType()) 
       { 
        <th>@item.Party</th> 
        <th>@item.Experience</th> 
        <th>b.d</th> 
        <th>b.d</th> 
       } 
       @if (item.GetType() == Model.AStudent.GetType()) 
       { 
        <th>b.d</th> 
        <th>b.d</th> 
        <th>@item.Major</th> 
        <th>@item.Year</th> 
       } 
       </tr> 
      } 
    </table> 
</div> 
+0

は、カスタムメイドのビューモデルを作成し、コントローラにそのモデルを構成します。ビュー –

答えて

0

まず、あなたは@usingディレクティブを経由してあなたのカミソリビューで名前空間をインポートすることができます。 @model指示の前にこれを行うことができます。次に、あなたのビューにあなたのタイプStudentsPoliticsを使用することができます。

第二には、itemは、具体的な型である場合、あなたはas演算子を使用することができ、その結果がnullがないかどうか確認します。したがって、あなたのインスタンスでGetType()に電話する必要はありません。 AStudent,APoliticARetiredのインスタンスを削除できるようになりました。

また、あなたのコードには2つのサイドノート:

  1. foreachループでdynamicを使用しないでください。ここに動的な型入力​​は必要ありません。代わりに、item変数のタイプとしてPersonを使用してください。これを行うにはAllPeopleList<IPerson>ではなくList<Person>でなければなりません。実際には、何も定義していないので、IPersonインターフェイスはまったく必要ありません。
  2. あなたのクラスに複数形を使用しないことをお勧めします(StudentsPolitics)。 Studentsの1つのインスタンスはです。の学生の1人ですので、クラスStudentに電話をかけてみませんか? Politicsと同じです。

すべてのすべては、あなたのビューは次のようになります。

@using ConferenceAttendees.Models 
@model ConferenceAttendees.Models.PersonLists 
<style> 
h6 { 
    display: inline 
} 
th{ 
    text-align:center; 

} 
</style> 
<div> 
    <table style="width:100%" align="left"> 
     <tr> 
      <th>Imie</th> 
      <th>Nazwisko</th> 
      <th>Wiek</th> 
      <th>Partia</th> 
      <th>Doswiadczenie</th> 
      <th>Kierunek</th> 
      <th>Rok</th> 
     </tr> 
      @foreach (Person item in Model.AllPeople) 
      { 
       var student = item as Student; 
       var politician = item as Politician; 
       <tr> 
       <th>@item.Name</th> 
       <th>@item.LastName</th> 
       <th>@item.Age</th> 
       @if (politician != null) 
       { 
        <th>@politician.Party</th> 
        <th>@politician.Experience</th> 
        <th>b.d</th> 
        <th>b.d</th> 
       } 
       @if (student != null) 
       { 
        <th>b.d</th> 
        <th>b.d</th> 
        <th>@student.Major</th> 
        <th>@student.Year</th> 
       } 
       </tr> 
      } 
    </table> 
</div> 
+0

から@ifステートメントを削除していただきありがとうございます!あなたが説明したものは、foreachの動的についての示唆にもかかわらずうまく機能します。私が動的にforeachのIPersonによって置き換えるとき、私はこのエラーを受け取ります: "メソッドグループ '名前'を非代理人タイプの 'オブジェクト'に変換できません。 –

+0

確かに、これは動作しません。私のせい。私は自分の答えを更新しました。 –

+0

私があなたの提案したように、私は次のエラーが発生しました: "Confference.Attendees.Model.Personに 'Party'(...)の定義が含まれていません" –

関連する問題