我有两个模型,如下所示。在视图中遇到问题,
- RelationType:保存诸如“母亲、父亲、小学教师、法定监护人……等”之类的数据。</li>
- 父母:保存有关母亲或父亲或小学教师等的个人资料信息。的一个孩子。
在 RelationType 模型中,
public class RelationType
{
public int RelationTypeID { get; set; }
public string RelationName { get; set; }
public virtual ICollection<Parent> Parents { get; set; }
}
在模型中,
public class Parent
{
public int ParentID { get; set; }
[Required,Display(Name="Family")]
public int FamilyID { get; set; }
[Required,Display(Name="Relationship")]
public int RelationTypeID { get; set; }
[Required, MaxLength(50), Display(Name = "First Name")]
public string FirstName { get; set; }
[MaxLength(50), Display(Name = "Middle Name")]
public string MiddleName { get; set; }
[MaxLength(50), Display(Name = "Last Name")]
public string LastName { get; set; }
[MaxLength(12), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:###-##-####}")]
public string SSN { get; set; }
[MaxLength(50)]
public string Email { get; set; }
public virtual Family Families { get; set; }
public virtual RelationType RelationTypes { get; set; }
}
在控制器中,
public ActionResult Index(int id)
{
ViewBag.Parents = db.ParentRepository.GetAllByRefID(p => p.FamilyID == id).ToList();
ViewBag.RelationTypes = db.RelationTypeRepository.GetAll().ToList();
return View();
}
在我的视图 [Index.cshtml] 页面中,
@model StudentEnrollmentViewModel
@{
ViewBag.Title = "Create / Update Childer’s parents information";
List<Model.Parent> parentsmodel = ViewBag.Parents;
List<Model.RelationType> relationtypes = ViewBag.RelationTypes;
}
<h2>Student Enrollment</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@foreach (var rel in relationtypes)
{
foreach (var itm in parentsmodel)
{
if (rel.RelationTypeID == itm.RelationTypeID)
{
<h2>@rel.RelationName 's Information</h2>
@Html.HiddenFor(model => itm.ParentID, new { Value = itm.ParentID })
<div class="span12">
<div class="span3">
@Html.LabelFor(model => itm.FirstName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.MiddleName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.LastName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.SSN)
</div>
</div>
<div class="span12">
<div class="span3">
@Html.EditorFor(model => itm.FirstName)
@Html.ValidationMessageFor(model => itm.FirstName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.MiddleName)
@Html.ValidationMessageFor(model => itm.MiddleName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.LastName)
@Html.ValidationMessageFor(model => itm.LastName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.SSN)
@Html.ValidationMessageFor(model => itm.SSN)
</div>
</div>
<div class="span12">
<div class="span4">
@Html.LabelFor(model => itm.Email)
</div>
</div>
<div class="span12">
<div class="span4">
@Html.EditorFor(model => itm.Email)
@Html.ValidationMessageFor(model => itm.Email)
</div>
</div>
}
else
{
<h4>@rel.RelationName Information not provided!</h4>
<div class="span12">
<div class="span3">
@Html.LabelFor(model => itm.FirstName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.MiddleName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.LastName)
</div>
<div class="span3">
@Html.LabelFor(model => itm.SSN)
</div>
</div>
<div class="span12">
<div class="span3">
@Html.EditorFor(model => itm.FirstName)
@Html.ValidationMessageFor(model => itm.FirstName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.MiddleName)
@Html.ValidationMessageFor(model => itm.MiddleName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.LastName)
@Html.ValidationMessageFor(model => itm.LastName)
</div>
<div class="span3">
@Html.EditorFor(model => itm.SSN)
@Html.ValidationMessageFor(model => itm.SSN)
</div>
</div>
<div class="span12">
<div class="span4">
@Html.LabelFor(model => itm.Email)
</div>
</div>
<div class="span12">
<div class="span4">
@Html.EditorFor(model => itm.Email)
@Html.ValidationMessageFor(model => itm.Email)
</div>
</div>
}
}
}
}
我的目标是将现有的父母(母亲和父亲......等)信息绑定到控件中,然后在修改时将其保存到数据库中。现有的父母信息来自 IF 块,如果父母信息不可用,则必须提供接口来创建新的父母(母亲/父亲/ ...),它将进入 ELSE 块。
在上面的代码(视图)中,我无法正确绑定父母的信息,应该按照母亲、父亲、小学老师等关系分类。像下面
找到父亲信息!
姓名:XXXXXX 姓氏:XXXXXX 中间名:XXXXXX SSN:XXXXXX 电子邮件:XXXXXX@xyz.com
未找到妈妈信息!![请在下方提供]
姓名:_ _姓氏:_ _中间名:_ _ SSN:_ _电子邮件:_
找到小学教师信息!
姓名:XXXXXX 姓氏:XXXXXX 中间名:XXXXXX SSN:XXXXXX 电子邮件:XXXXXX@xyz.com
未找到法定监护人信息!![请在下方提供]
姓名:_ _姓氏:_ _中间名:_ _ SSN:_ _电子邮件:_
其他一些关系……等等。[请在下方提供]
姓名:_ _姓氏:_ _中间名:_ _ SSN:_ _电子邮件:_
节省
通过点击保存按钮,上面提供的信息应该被创建/更新到数据库中。我已经有创建/更新的代码。但是我在显示带有/不带数据的控件时遇到的问题应该按关系类型分类
请你帮助我好吗 ?
提前致谢。