나는 ASP.NET MVC를 사용하는 것이 상대적으로 처음이므로 여기서 간단한 것을 간과하고 있다면 내 무지를 용서하십시오. 그러나 공정한 연구 끝에 여전히 확실한 해결책을 찾지 못했습니다.
내 컨트롤러의 프로젝트 목록을 IEnumerable Project 유형의 뷰로 전달하고 있습니다.
IEnumerable<Project> Projects = ViewBag.Projects;
그런 다음 각 프로젝트를 반복하고 정보로 Datatable을 채 웁니다. 유일한 문제는 루프 내에서 두 번째 DB 쿼리를 만들어야하는 Datatable의 두 열에 대한 데이터를 가져 오는 것입니다.
특정 프로젝트와 관련된 아카이브 목록을 반환해야하는 내 저장소에서 메서드를 호출하기 위해 @functions 섹션을 만들었습니다.
보기 상단
@using stwintranet.Areas.PQP.Models
@model Year
@{
Layout = "~/Views/Shared/_Layout.cshtml";
IEnumerable<Project> Projects = ViewBag.Projects;
IEnumerable<Year> Years = ViewBag.Years;
String year = ViewBag.URLYear;
}
@functions
{
private readonly IArchive_ElectronicRepository Archive_ElectronicRepository;
public IEnumerable<Archive_Electronic> GetListArchives(int pid)
{
return Archive_ElectronicRepository.GetListArchives(pid);
}
}
메인보기 :
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<tr>
<th>Project Year</th>
<th>Project Number</th>
<th>Project Title</th>
<th>Client</th>
<th>Site Address</th>
<th>Director</th>
<th>Archive Status</th>
<th>Primary Location</th>
<th>Location</th> <!-- Requires 2nd Query -->
<th>Documents</th> <!-- Requires 2nd Query -->
</tr>
</thead>
<tbody>
@foreach (var project in Projects)
{
//Get a list of archived projects
IEnumerable<Archive_Electronic> ArchiveProjects = GetListArchives(project.ProjectID);
string Docs = "";
string Locs = "";
foreach (var archive in ArchiveProjects)
{
//Write List of Docs
if (archive.DocumentType == 1)
{
Docs = Docs + "Dwg ";
}
else if (archive.DocumentType == 2)
{
Docs = Docs + "Doc ";
}
else if (archive.DocumentType == 3)
{
Docs = Docs + "Eml ";
}
//Write List of Locations
if (archive.Location == 2)
{
Locs = Locs + "Dub ";
}
else if (archive.Location == 3)
{
Locs = Locs + "Gal ";
}
else if (archive.Location == 4)
{
Locs = Locs + "Lon ";
}
else if (archive.Location == 6)
{
Locs = Locs + "Cor ";
}
}
<tr id="@project.ProjectNumber" onclick="showProject(this.id)">
<td>@project.Year</td>
<td>@project.ProjectNumber</td>
<td>@project.ProjectTitle</td>
<td>@project.Organisation.Organisation1</td>
<td>@project.SiteAddress1</td>
<td>@project.ProjectDirector</td>
<td>Archive Status will go here</td>
<td>@project.SiteArea</td>
<td>@Locs</td>
<td>@Docs</td>
</tr>
}
</tbody>
</table>
저장소 내에있는 방법 :
public IEnumerable<Archive_Electronic> GetListArchives(int id)
{
return context.Archive_Electronic.Where(x => x.Project == id).DefaultIfEmpty();
}
제어 장치:
// GET: PQP/Stage9/ElectronicArchiveReport
public ActionResult ElectronicArchiveReport(string id)
{
ViewBag.Years = YearRepository.GetYears();
ViewBag.Projects = ProjectRepository.GetListProjects(id);
ViewBag.URLYear = id;
return View();
}
이 코드를 그대로 사용하면 다음을 얻을 수 있습니다.
System.NullReferenceException: Object reference not set to an instance of an object.
보기에있는 반품 :
return Archive_ElectronicRepository.GetListArchives(pid);
@functions에 위치한, 나는 당신이 아마도 이와 같이 DB를 호출해서는 안된다는 것을 알고 있습니다. 이것이 아마도 null 예외 오류가 발생하는 이유 일 것입니다.하지만 여기에서 손실을 입었고 실제로이 작업을 수행하는 적절한 방법을 찾을 수 없습니다.
귀하는 private readonly IArchive_ElectronicRepository Archive_ElectronicRepository;
인스턴스화 또는 할당하지 않는 것 같습니다되는 결과NullReferenceException
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다