我创建了一个MVCApplication1,因为有两个这样的模型类,
StudentPersonalInfo.cs
public class StudentPersonalInfo
{
public string StudentFirstName { get; set; }
public string StudentLastName { get; set; }
public int Age { get; set; }
public string BloodGroup { get; set; }
public string Address { get; set; }
}
第二个是
EducationQualification.cs
public class EducationQualification
{
public string Graduation { get; set; }
public intGrad_Marks_obtain { get; set; }
public DateTimeGrad_passing_year { get; set; }
public stringPostGraduation { get; set; }
public intPG_Marks_obtain { get; set; }
public DateTimePG_passing_year { get; set; }
}
我想在一个视图页面中显示两个模型数据,并且在创建这两个模型后我不知道下一步该做什么。
任何人都可以帮我吗?
答案 0 :(得分:0)
您可以根据模型创建对象:
StudentPersonalInfo myStudent = new StudentPersonalInfo();
EducationQualification myQualification = new EducationQualification();
你可以更具体一点吗?你可以做几件事,你可以用很多其他方式来操纵它们。
答案 1 :(得分:0)
public class StudentPersonalInfo
{
public string StudentFirstName { get; set; }
public string StudentLastName { get; set; }
public int Age { get; set; }
public string BloodGroup { get; set; }
public string Address { get; set; }
}
public class EducationQualification
{
public string Graduation { get; set; }
public int Grad_Marks_obtain { get; set; }
public DateTime Grad_passing_year { get; set; }
public string PostGraduation { get; set; }
public int PG_Marks_obtain { get; set; }
public DateTime PG_passing_year { get; set; }
}
public class StudentInfo
{
public StudentPersonalInfo PersonalInfo {get;set;}
public EducationQualification EducationalQualification {get;set;}
}
现在在视图中使用 StudentInfo 类作为模型。请参阅视图中的聚合类:
@Html.TextBoxFor(m=>m.StudentPersonalinfo.StudentFirstname)
<强>更新强>
学生 - 模特
public class StudentPersonalInfo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string StudentFirstName { get; set; }
public string StudentLastName { get; set; }
public int Age { get; set; }
public string BloodGroup { get; set; }
public string Address { get; set; }
}
public class EducationQualification
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Graduation { get; set; }
public int Grad_Marks_obtain { get; set; }
public DateTime Grad_passing_year { get; set; }
public string PostGraduation { get; set; }
public int PG_Marks_obtain { get; set; }
public DateTime PG_passing_year { get; set; }
}
public class StudentInfo
{
public StudentPersonalInfo PersonalInfo { get; set; }
public EducationQualification EducationalQualification { get; set; }
}
<强> StudentsDbContext.cs 强>
public class StudentDbContext:DbContext
{
public DbSet<StudentPersonalInfo> StudentPersonalInfos { get; set; }
public DbSet<EducationQualification> EducationQualifications { get; set; }
}
<强>控制器强>
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(StudentInfo model)
{
var personalInfo = model.PersonalInfo;
var educationQualification = model.EducationalQualification;
using (var context = new StudentDbContext())
{
context.StudentPersonalInfos.Add(personalInfo);
context.EducationQualifications.Add(educationQualification);
context.SaveChanges();
}
return View();
}
查看强>
@using (Html.BeginForm())
{
<div style="width:500px;margin:auto;">
<fieldset>
<legend>Student Personal Info</legend>
<p>Student First Name: @Html.TextBoxFor(m => m.PersonalInfo.StudentFirstName)</p>
<p>Student Last Name: @Html.TextBoxFor(m => m.PersonalInfo.StudentLastName)</p>
<p>Age: @Html.TextBoxFor(m => m.PersonalInfo.Age)</p>
<p>Blood Group: @Html.TextBoxFor(m => m.PersonalInfo.BloodGroup)</p>
<p>Address: @Html.TextAreaFor(m => m.PersonalInfo.Address)</p>
</fieldset>
<fieldset>
<legend>Educational Qualification</legend>
<p>Graduation: @Html.TextBoxFor(m => m.EducationalQualification.Graduation)</p>
<p>Grad. Marks Obtained: @Html.TextBoxFor(m => m.EducationalQualification.Grad_Marks_obtain)</p>
<p>Grad. Passing Year: @Html.TextBoxFor(m => m.EducationalQualification.Grad_passing_year)</p>
<p>PG: @Html.TextBoxFor(m => m.EducationalQualification.PostGraduation)</p>
<p>PG Marks Obtained: @Html.TextBoxFor(m => m.EducationalQualification.PG_Marks_obtain)</p>
<p>PG Passing Year: @Html.TextBoxFor(m => m.EducationalQualification.PG_passing_year)</p>
</fieldset>
<input type="submit" value="Save"/>
</div>
}
您可以根据自己的喜好自由进行验证并添加样式。此演示仅供参考。
以下是基于您的类的示例项目,用于演示目的:
https://github.com/sunnykumar08/public
希望它有所帮助!