我正在尝试在ASP.NET MVC模型绑定中使用复杂的对象类型。一世 需要插入和更新学生记录与考试结果
public class InitialApplicantLevel2Model
{
public InitialApplicantLevel2Model()
{
alResults = new ALResults();
}
public int InitialApplicantLevel2ID { get; set; }
public int? IntakeID { get; set; }
public string ApplicantName { get; set; }
public string ContactNumber { get; set; }
public string School { get; set; }
public string NIC { get; set; }
public string Passport { get; set; }
public string Sex { get; set; }
public ALResults alResults { get; set; }
}
public class ALResults
{
public int ResultsID { get; set; }
public int InitialApplicantLevel2ID { get; set; }
public string Stream { get; set; }
public string Grading { get; set; }
public string IndexNo { get; set; }
public int? Year { get; set; }
public int? Attempt { get; set; }
public double? ZScore { get; set; }
public string Medium { get; set; }
}
存储库类创建新记录
public bool Create(InitialApplicantLevel2Model model)
{
try
{
using (context = new SIMSDBAPPEntities())
{
using (TransactionScope scope = new TransactionScope())
{
tblInitialApplicantLevel2 tb = new tblInitialApplicantLevel2();
tb.NIC = model.NIC;
tb.Passport = model.Passport;
tb.Sex = model.Sex;
context.tblInitialApplicantLevel2.Add(tb);
tblALResult results = new tblALResult();
results.Attempt = model.alResults.Attempt;
results.Grading = model.alResults.Grading;
results.IndexNo = model.alResults.IndexNo;
results.InitialApplicantLevel2ID = tb.InitialApplicantLevel2ID;
results.Stream = model.alResults.Stream;
results.Year = model.alResults.Year;
results.Medium = model.alResults.Medium;
results.ZScore = model.alResults.ZScore;
context.tblALResults.Add(results);
Save();
scope.Complete();
}
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
更新记录
public bool Update(InitialApplicantLevel2Model model)
{
try
{
using (context = new SIMSDBAPPEntities())
{
using (TransactionScope scope = new TransactionScope())
{
var tb = (from tba in context.tblInitialApplicantLevel2
where tba.InitialApplicantLevel2ID == model.InitialApplicantLevel2ID
select tba).SingleOrDefault();
tb.NIC = model.NIC;
tb.Passport = model.Passport;
tb.Sex = model.Sex;
var results = (from tbr in context.tblALResults
where tbr.InitialApplicantLevel2ID == model.InitialApplicantLevel2ID
select tbr).SingleOrDefault();
results.Attempt = model.alResults.Attempt;
results.Grading = model.alResults.Grading;
results.IndexNo = model.alResults.IndexNo;
results.InitialApplicantLevel2ID = tb.InitialApplicantLevel2ID;
results.Stream = model.alResults.Stream;
results.Year = model.alResults.Year;
results.Medium = model.alResults.Medium;
results.ZScore = model.alResults.ZScore;
Save();
scope.Complete();
}
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
查看
<tr><td colspan="6"><hr /><b>G.C.E. (A/L) Results</b></td></tr>
<tr>
<td>Year: </td>
<td>@Html.TextBoxFor(o=>o.alResults.Year)</td>
<td>Index No: </td>
<td>@Html.TextBoxFor(o=>o.alResults.IndexNo)</td>
<td>Medium: </td>
<td>@Html.TextBoxFor(o=>o.alResults.Medium)</td>
</tr>
<tr>
<td>Stream: </td>
<td>@Html.TextBoxFor(o=>o.alResults.Stream)</td>
<td>Attempt: </td>
<td>@Html.TextBoxFor(o=>o.alResults.Attempt)</td>
<td>Zscore: </td>
<td>
@Html.TextBoxFor(o=>o.alResults.ZScore)
</td>
</tr>
学生详细信息插入时没有问题,但ALResults 属性为null。但是,更新记录ALResults对象时 properties从视图中获取值并完美地更新记录。 插入记录时为什么对象属性为空?
答案 0 :(得分:0)
请尝试如下所示。使用“alResults”对象使用“virtual”。
public class InitialApplicantLevel2Model
{
//removed for clarity
public virtual ALResults alResults { get; set; }
}