2011-03-13 23 views
0

我的問題,當我換三類 第一類是開始的DropDownList在包裝類兩類

[Bind(Exclude="ID")] 
public class Material 
{ 
    public string MaterialName { get; set; } 
} 

第二類是

[Bind(Exclude="ID")] 
public class ExameState 
{ 
    public string ExamState { get; set; } 
} 

第三類是

[Bind(Exclude="ID")] 
public class Exams 
{ 
    public string ExamsName { get; set; } 
    public DateTime CreationDate { get; set; } 
    public DateTime StartingDate { get; set; } 
    public int Period { get; set; } 
    public int ExamStateID { get; set; } 
    public int MaterialID { get; set; } 
    public int GroupID { get; set; } 
    public int QuestionState { get; set; } 
    public int TeacherID { get; set; } 
    public int ExamMarkState { get; set; } 
    public string Password { get; set; } 
} 

包裝類是

public class Examswrapper 
{ 
    public Material material { get; set; } 
    public ExameState examstate { get; set; } 
    public Exam exam { get; set; } 
} 

我需要datavalue = MaterialName 並鑑於關鍵= ID建立在Examswrapper類顯示下拉列表物料 我想這

如何使它 ,感謝您的諮詢

新錯誤: System.NullReferenceException:未將對象引用設置爲對象的實例。

+0

存在類材料沒有身份證,缺少什麼? – 2011-03-13 10:12:22

回答

0

沒有您的視圖模型一個問題,因爲Material屬性應該是一個集合,如果你想有一個下拉列表中。此外您的材料類缺少ID屬性。所以:

public class Material 
{ 
    public string ID { get; set; } 
    public string MaterialName { get; set; } 
} 

public class ExamsViewModel 
{ 
    public string SelectedMaterialId { get; set; } 
    public IEnumerable<Material> Materials { get; set; } 

    ... // some other properties 
} 

,然後在你的強類型的視圖:

<%= Html.DropDownListFor(
    // This is the property that will hold the selected value 
    x => x.SelectedMaterialId, 
    // Build the select list based on the Model.Materials collection 
    new SelectList(
     Model.Materials.Select(material => new SelectListItem 
     { 
      Value = material.ID, 
      Text = material.MaterialName 
     }), 
     "Value", "Text" 
    ) 
) %> 
+0

有一個錯誤:方法'選擇'沒有超載需要3個參數 – Abdalmohaymen 2011-03-13 13:44:28

+1

@Abdalmohaymen,的確,我犯了一個錯誤。我已更新我的帖子。 – 2011-03-13 13:50:57

+0

問題已更新。 – Abdalmohaymen 2011-03-13 14:30:58

0
@Html.DropDownListFor(model => model.material, 
     Model.material.Select(
     x => new SelectListItem 
     { 
      Text = x.MaterialName, 
      Value = x.Id 
     } 
    ))