Asp.net mvc 两个类的包装类中的DropDownList
我的问题是从我结束三节课开始的 第一节课是Asp.net mvc 两个类的包装类中的DropDownList,asp.net-mvc,Asp.net Mvc,我的问题是从我结束三节课开始的 第一节课是 [Bind(Exclude="ID")] public class Material { public string MaterialName { get; set; } } [Bind(Exclude="ID")] public class Exams { public string ExamsName { get; set; } public DateTime CreationDate { get; set; }
[Bind(Exclude="ID")]
public class Material
{
public string MaterialName { 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; }
}
第二节课是
[Bind(Exclude="ID")]
public class ExameState
{
public string ExamState { get; set; }
}
第三类是
[Bind(Exclude="ID")]
public class Material
{
public string MaterialName { 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; }
}
包装类是
[Bind(Exclude="ID")]
public class Material
{
public string MaterialName { 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的物料显示dropdownlist
在Examswrapper类上构建的视图中
我正在尝试这个
怎么做
谢谢你的建议
新错误:
System.NullReferenceException:对象引用未设置为对象的实例。视图模型存在问题,因为如果需要下拉列表,则
材质属性应为集合。此外,材质类缺少ID属性。因此:
@Html.DropDownListFor(model => model.material,
Model.material.Select(
x => new SelectListItem
{
Text = x.MaterialName,
Value = x.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
}
公共类材料
{
公共字符串ID{get;set;}
公共字符串MaterialName{get;set;}
}
公共类ExamView模型
{
公共字符串SelectedMaterialId{get;set;}
公共IEnumerable材质{get;set;}
…//其他一些属性
}
然后在强类型视图中:
<%= 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"
)
) %>
x.SelectedMaterialId,
//基于Model.Materials集合构建选择列表
新选择列表(
Model.Materials.Select(material=>newselectListItem
{
值=material.ID,
Text=material.MaterialName
}),
“值”、“文本”
)
) %>
类材料中没有Id,丢失?有一个错误:方法“Select”没有重载3arguments@Abdalmohaymen是的,的确,我犯了一个错误。我已经更新了我的帖子。@Abdalmohaymen,您是否在控制器操作中初始化了材料集合?