提前感谢您收听我的问题。
我有一个Linq to Sql Class,我在网格上显示了特定的属性,我使用了ViewModel。
首先,我将通过部分课程粘贴我扩展课程的部分:
public string Owner { get; private set; }
public Int32 Documents { get; private set; }
partial void OnLoaded()
{
Owner = "PersonCompanyRoleMedicalExam";
Documents = sfdb.StoredFiles.Count(s => s.Owner == this.Owner && s.OwnerId == this.Id);
}
接下来粘贴我用于网格的ViewModel:
public class MedicalExamViewModel
{
public Int32 Id { get; set; }
public Int32 PersonCompanyRoleId { get; set; }
public Int32? PeriodInDays { get; set; }
public DateTime? ExamCompleted { get; set; }
public bool? MedicallyFit { get; set; }
public Int32 Documents { get; set; }
}
然后最后我的方法从我的网格填充ajax来填充它:
[GridAction]
public ActionResult _MedicalExamGridAjaxBinding(Int32 Id)
{
PersonCompanyRole personCompanyRole = db.PersonCompanyRoles.Single(p => p.PersonId == Id);
var model = from o in db.PersonCompanyRoleMedicalExams
where o.PersonCompanyRoleId == personCompanyRole.Id
select new MedicalExamViewModel
{
Id = o.Id,
PersonCompanyRoleId = o.PersonCompanyRoleId,
PeriodInDays = o.PeriodInDays,
ExamCompleted = o.ExamCompleted,
MedicallyFit = o.MedicallyFit,
Documents = o.Documents
};
return View(new GridModel
{
Data = model
});
}
在“var model = from db in db.PersonCompanyRoleMedicalExams”中我刚刚破解,查看对象我得到了基础{System.SystemException} = {“类成员PersonCompanyRoleSHEAppointment.Documents未映射。”}
深入挖掘对象到System.SystemException异常中,在InnerException中它声明如下:_COMPlusExceptionCode = -532462766。
有趣的是,我使用完全相同的技术在我的项目中填充其他对象/模型没有任何问题,但有一个区别,那就是在它们之间有一个链接表。这样的事情:人 - > PersonCompanyRole> PersonCompanyRoleMedicalExam。 PersonCompanyRole的人是一对一的关系,PersonCompanyRole与PersonCompanyRoleMedicalExam是一对多的关系。为了完整性,在我的ajax方法中粘贴,我没有任何例外。
[GridAction]
public ActionResult _IncidentsGridAjaxBinding(Int32 Id)
{
var structures = db.sp_GetCompanyStructureDecendants(Id);
Collection<CompanyStructureIncident> companyStructureIncidents = new Collection<CompanyStructureIncident>();
foreach (sp_GetCompanyStructureDecendantsResult decendant in structures)
{
IEnumerable<CompanyStructureIncident> equipment = db.CompanyStructureIncidents.Where(r => r.CompanyStructureId == decendant.Id);
foreach (CompanyStructureIncident companyStructureIncident in equipment)
{
companyStructureIncidents.Add(db.CompanyStructureIncidents.Single(p => p.Id == companyStructureIncident.Id));
}
}
var model = from o in companyStructureIncidents
//where o.CompanyStructureId == Id
select new IncidentViewModel
{
Id = o.Id,
CompanyStructureId = o.CompanyStructureId,
DateOfOccurence = o.DateOfOccurence,
DateReported = o.DateReported,
Documents = o.Documents
};
return View(new GridModel
{
Data = model
});
}
希望你能提供帮助。
答案 0 :(得分:0)
我想通了,而不是使用OnLoad()我这样做:
public string Owner
{
get { return "PersonCompanyRoleMedicalExam"; }
}
public Int32 Documents
{
get { return sfdb.StoredFiles.Count(s => s.Owner == this.Owner && s.OwnerId == this.Id); }
}
非常感谢,希望这会有助于其他人。