Thanks I have added an entity tag will also add a linq to sql tag since that is what I am using for my ORM. I have solved the issue but I feel it was a bit hack. What I currently have implemented is another model that defines the expected properties and from here I populate it with the GetContacts() method and everything works as expected. Again it feels a bit hack.
Linq query for view returns no elements
-
30-06-2022 - |
문제
Currently, I am trying to populate additional values within the same ViewModel. So what happens is my viewmodel is populated initially via this JsonResult method.
public virtual JsonResult GetInstitutionContacts(Guid? provideruid)
{
return Json(new { ContactViewModel = _repository.GetContactsByProviderUid(provideruid) }, JsonRequestBehavior.AllowGet);
}
The parameter value is set when a user makes a selection from a dropdown list. The repository that my JsonResult method utilizes looks like this:
public IQueryable<ContactViewModel> GetContactsByProviderUid(Guid? provideruid)
{
var q = from r in Context.vw_DistrictContacts.Where(p => p.ProviderUid == provideruid && p.School == AdminBuilding)
select new ContactViewModel
{
FamilyName = r.GivenNames.ToUpper(),
GivenName = r.FamilyNames.ToUpper(),
CourtesyTitle = r.Title,
ContactId = r.PersonUid
};
return q.Distinct();
}
The table view within my context (Context.vw_DistrictContacts) returns those exact fields(FamilyName, GiveName...). The issue occurs when the user then selects another value. The dropdown selection then triggers this controller method:
public virtual JsonResult GetSchoolContacts(Guid provideruid)
{
return Json(new { ContactViewModel = _repository.GetContacts(provideruid) }, JsonRequestBehavior.AllowGet);
}
This is what the repository method looks like:
public List<ContactViewModel> GetContacts(Guid? providerUid)
{
var q = from r in Context.vw_PersonToSdeUsers.Where(p => p.ProviderUid == providerUid)
select new ContactViewModel
{
SchoolName = r.SchoolName,
ContactId = r.PersonUid,
PersonUid = r.PersonUid,
FamilyName = r.GivenNames.ToUpper(),
GivenName = r.FamilyNames.ToUpper(),
SdeUserEmail = r.SdeUserEmail,
CourtesyTitle = r.ProgramDescription
};
return q.ToList();
}
Again the table view (Context.vw_PersonToSdeUsers) returns exactly what is expected. But the return value "q.ToList()" is null. I believe the issue is the fact that I am already consuming ContactViewModel. Also I have ran the select statement for the table view and it returns as expected. I have also ran the provider filter and again it returns as expected.
해결책