有一种表格,我将要求学术详情:
到目前为止,任何用户都说UserId =“1”将在我的AcademicMaster中创建3个条目,分别用于学士,硕士(毕业后)和专业资格。
我的数据库AcademicMaster表字段和datamodel:
Id,Qualification(GraduationCourses),Acheievement,UserId
查看型号:
public class AcademicViewModel
{
public int Id { get; set; }
public virtual Graduation Graduation{ get; set; }
public virtual PostGraduation PostGraduation{ get; set; }
public virtual ProfessionalQualification ProfessionalQualification{ get; set; }
}
public class Graduation
{
public string BachelorQualification { get; set; }
public string BachelorAchievement { get; set; }
}
public class PostGraduation
{
public string MasterQualification { get; set; }
public string MasterAchievement { get; set; }
}
public class ProfessionalQualification
{
public string ProfessionalQualifications { get; set; }
}
所以我的观点是这样的:
@model AcademicViewModel
@{
IEnumerable<SelectListItem> graduationList = ViewBag.GraduationList;
IEnumerable<SelectListItem> postGraduationList = ViewBag.PostGraduationList;
}
@using (Html.BeginForm())
{
<div class="row">
Bachelors
@Html.DropDownListFor(model => model.Graduation.Qualification, graduationList)
</div>
@Html.TextAreaFor(model => model.Graduation.Achievement)
<div class="row">
MASTERS
@Html.DropDownListFor(model => model.PostGraduation.Qualification, postGraduationList)
</div>
@Html.TextAreaFor(model => model.PostGraduation.Achievement)
<div class="row">
PROFESSIONAL QUALIFITCATION
@Html.TextAreaFor(model => model.ProfessionalQualification.ProfessionalQualifications)
</div>
<input type="submit" value="Save">
}
这是我的控制器:
[HttpPost]
public ActionResult MyController(AcademicViewModel model)
{
//Actions
}
我的视图模型结构是否合适以及如何在AcademicMaster表中创建3个条目?
答案 0 :(得分:2)
我首先要说的是,拥有一个表可能不是最佳选择(如果稍后您开始添加可能适用于Graduation
但不适用于Professional
的其他属性会发生什么情况 - 对于示例YearOfGraduation
- 您最终可能会有大量字段,其中许多字段可能有null
个值。
但是,如果您想要一个表,那么至少要添加另一个字段,以便您可以确定数据是否与Graduation
,PostGraduation
或Professional
相关。 AcademicMasters
表的关联数据模型将是
public class AcademicMaster
{
public int ID { get; set; }
public string Type { get; set; } // may be an enum?
public string Qualification { get; set; }
public string Achievement { get; set; }
public int UserID { get; set; }
}
附注:对enum
属性使用Type
可能更好
public enum AcademicType
{
Graduation,
PostGraduation,
Professional
}
您当前的Graduation
,PostGraduation
和ProfessionalQualification
模型似乎没有必要,您的视图模型应该是
public class AcademicViewModel
{
public string GraduationQualification { get; set; }
public string GraduationAchievement { get; set; }
public string PostGraduationQualification { get; set; }
public string PostGraduationAchievement { get; set; }
public string ProfessionalAchievement { get; set; }
public IEnumerable<SelectListItem> GraduationList { get; set; }
public IEnumerable<SelectListItem> PostGraduationList { get; set; }
}
附注:不清楚当前ProfessionalQualifications
属性是什么 - 是否会将其分配给数据库中的Qualification
字段或Acheievement
字段?由于您使用的是视图模型,因此它应该包含SelectList
,而不是使用ViewBag
。
然后你的观点将是
@model AcademicViewModel
@using (Html.BeginForm())
{
<h2>Graduation</h2>
@Html.DropDownListFor(m => m.GraduationQualification, Model.GraduationList)
@Html.TextAreaFor(m => m.GraduationAchievement)
... // repeat for PostGraduation and Professional
<input type="submit" value="Save">
}
POST方法将是
[HttpPost]
public ActionResult MyController(AcademicViewModel model) // should be named Create?
{
var userID = ....
AcademicMaster graduation = new AcademicMaster
{
Type = AcademicType.Graduation,
Qualification = model.GraduationAchievement,
Achievement = model.GraduationAchievement,
UserId = userID;
};
db.AcademicMasters.Add(graduation);
// Repeat for PostGraduation and Professional
db.SaveChanges();
// redirect?
}