2012-01-20 13 views
0

から私は、次のようにEntity Frameworkの(私は手で何かを作るわけではない)によって、既存のデータベースから作成したクラスの生徒ていません:MVC DropDownListForとコレクションは、モデル

また
public class student 
{ 
    ... 
    public int student_status { get; set; } 
    ... 
} 

私は、ナビゲーションプロパティによって(リンクされています以下のような学生のステータスの)コレクション:

public class student_status 
{ 
    public int id { get; set; } 
    public string status { get; set; } 
} 

私は(System.Web.Mvc.ViewUserControl <EntityTypes.Models.Student>)強く型付けされたビューを使用して、私はDROを表示したいですスタッド・ステータスと選択されたpダウン・リスト - 既存。

注:studentエンティティitselsにはすべてのステータスのコレクションが含まれていません。つまり、リストのデータはモデルには表示されません。ViewDataを使用して渡すことができます。

私はHtml.DropDownListFor(x => x.student_status、ViewData ["StudentStatuses"]をIEnumerable、htmlAttrsとして使用しようとしましたが、失敗しました(ViewData ["StudentStatuses"]はコントローラコレクションで準備されています)。

ナビゲーションプロパティを使用してもいいですか?私は理解しようとしました。インクルード()命令は運がありません。

どうすればいいですか?

答えて

1

あなたのモデルはそれなりのものです。私の例では、スペル/構文を変更しました。コピーして貼り付ける場合は、調整する必要があります。ここで

はActionMethodsです:

 [HttpGet] 
     public ActionResult Index() 
      { 
       StudentModel model = new StudentModel(); 
       List<StudentStatus> StatusList = new List<StudentStatus>(); 
       StatusList.Add(new StudentStatus { Id = 1, Name = "In School" }); 
       StatusList.Add(new StudentStatus { Id = 2, Name = "Out of School" }); 
       ViewData["StatusList"] = StatusList; 
       return View(model); 
      } 

     [HttpPost] 
     public ActionResult Index(StudentModel model) 
     { 
      return View(); 
     } 

ビュー:

@Html.DropDownListFor(m => m.SelectedStatus, new SelectList(ViewData["StatusList"] as System.Collections.IEnumerable, "Id", "Name", @Model.SelectedStatus), "Select Status") 

モデル:

public class StudentModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int SelectedStatus { get; set; } 
} 



    public class StudentStatus 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
    } 
+0

うん!それだ! –

関連する問題