我有两个型号,如下所示。在View中获取问题,
在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块中。
在上面的代码(View)中,我无法正确绑定父信息,应该按照母亲,父亲,小学教师等关系对其进行分类。如下所示
找到父亲信息!
名称:XXXXXX姓氏:XXXXXX中间名:XXXXXX SSN:XXXXXX电子邮件:XXXXXX@xyz.com
未找到母亲信息!! [请在下方提供]
名称: _ _ 姓氏: _ _ 中间名: _ _ SSN: _ _ 电子邮件: _
找到小学教师信息!
名称:XXXXXX姓氏:XXXXXX中间名:XXXXXX SSN:XXXXXX电子邮件:XXXXXX@xyz.com
未找到法律监护人信息!! [请在下方提供]
名称: _ _ 姓氏: _ _ 中间名: _ _ SSN: _ _ 电子邮件: _
其他一些关系等等。 [请在下方提供]
名称: _ _ 姓氏: _ _ 中间名: _ _ SSN: _ _ 电子邮件: _
保存
单击SAVE按钮,上面提供的信息应该是创建/更新到数据库中。我已经有了Create / Update的代码。但我在显示带/不带数据的控件时出现的问题应该按关系类型分类
你能帮我吗?
提前致谢。
答案 0 :(得分:0)
你的意思是不能正确绑定信息?如果我理解正确,您正在尝试查找某个人并确定该人的个人资料信息是否已经有母亲/父亲/教导/等。信息,对吗?如果是这样,我们可以修复模型并轻松实现。 (将你的模型命名为父母会让我失望一点,但也许我不明白你想要实现的目标。我道歉。)