I got error object reference not set to an instance of an object in razor view when click details button
what I add new in condition :
r.Validated == 2
This is the controller code :
public ActionResult Details(int id , int programId, int custId , int MachineId)
{
int UserId = Convert.ToInt32(Session["UserID"]);
var results = _context.RESULTS.Where(r => r.custid == UserId && r.sample_id == id && r.status == 2 && r.program_id == programId && r.custid == custId && MachineId == r.machine_id && r.Validated == 2).ToList();
return View(results);
}
what I add new this line :
Model.FirstOrDefault().Validated == 2
This is the view code :
@model IEnumerable<warehouse.Models.RESULT>
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
@if (Model.FirstOrDefault().program_id < 6 && Model.FirstOrDefault().Validated == 2)
{
<img style="margin-left:250px;" src="~/images/weblogo.png" />
<div style="margin-left:50px;"> @Html.ActionLink("Back to List", "Indexs", "HospitalSamples") </div>
<table class="table">
<tr style="background-color:hotpink;text-align:left">
<th>@Html.DisplayNameFor(model => model.sample.name)</th>
<th>@Html.DisplayNameFor(model => model.Program.name)</th>
<th>@Html.DisplayNameFor(model => model.LabTest.TestName)</th>
<th>@Html.DisplayNameFor(model => model.APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(model => model.RESULT1)</th>
<th>@Html.DisplayNameFor(model => model.mean)</th>
<th>@Html.DisplayNameFor(model => model.sd)</th>
<th>@Html.DisplayNameFor(model => model.sdi)</th>
<th>@Html.DisplayNameFor(model => model.low_limit)</th>
<th>@Html.DisplayNameFor(model => model.high_limit)</th>
<th>@Html.DisplayNameFor(model => model.Machine.Machine_name)</th>
<th>@Html.DisplayNameFor(model => model.performance.name)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.sample.name)</td>
<td>@Html.DisplayFor(modelItem => item.Program.name)</td>
<td>@Html.DisplayFor(modelItem => item.LabTest.TestName)</td>
<td>@Html.DisplayFor(modelItem => item.APPROVED_DATE)</td>
<td>@Html.DisplayFor(modelItem => item.RESULT1)</td>
<td>@Html.DisplayFor(modelItem => item.mean)</td>
<td>@Html.DisplayFor(modelItem => item.sd)</td>
<td>@Html.DisplayFor(modelItem => item.sdi)</td>
<td>@Html.DisplayFor(modelItem => item.low_limit)</td>
<td>@Html.DisplayFor(modelItem => item.high_limit)</td>
<td>@Html.DisplayFor(modelItem => item.Machine.Machine_name)</td>
<td>@Html.DisplayFor(modelItem => item.performance.name)</td>
</tr>
}
</table>
}
else if (Model.FirstOrDefault().program_id == 6 && Model.FirstOrDefault().Validated == 2)
{
<img style="margin-left:250px;" src="~/images/weblogo.png" />
<div style="margin-left:50px;"> @Html.ActionLink("Back to List", "Indexs", "HospitalSamples") </div>
<table class="table">
<tr style="background-color:hotpink;text-align:left">
<th>@Html.DisplayNameFor(model => model.sample.name)</th>
<th>@Html.DisplayNameFor(model => model.Program.name)</th>
<th>@Html.DisplayNameFor(model => model.LabTest.TestName)</th>
<th>@Html.DisplayNameFor(model => model.APPROVED_DATE)</th>
<th>@Html.DisplayNameFor(model => model.RESULT1)</th>
<th>@Html.DisplayNameFor(model => model.accepted_BG)</th>
<th>@Html.DisplayNameFor(model => model.Machine.Machine_name)</th>
<th>@Html.DisplayNameFor(model => model.performance.name)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.sample.name)</td>
<td>@Html.DisplayFor(modelItem => item.Program.name)</td>
<td>@Html.DisplayFor(modelItem => item.LabTest.TestName)</td>
<td>@Html.DisplayFor(modelItem => item.APPROVED_DATE)</td>
<td>@Html.DisplayFor(modelItem => item.RESULT1)</td>
<td>@Html.DisplayFor(modelItem => item.accepted_BG)</td>
<td>@Html.DisplayFor(modelItem => item.Machine.Machine_name)</td>
<td>@Html.DisplayFor(modelItem => item.performance.name)</td>
</tr>
}
</table>
}
This is the details button actionlink
@Html.ActionLink("Report", "Details", new { id = item.sample_id, programId = item.program_id, custId = item.custid , MachineId = item.machine_id }, new { @class = "btn btn-danger" })
when click the button I got the error in this line of code in view :
@if (Model.FirstOrDefault().program_id < 6 && Model.FirstOrDefault().Validated == 2)
how to solve this error what I need to check ?
UPDATE : see the image
now this error appeared if Validated = 1 in this line when I check if validated = 2 open the view how to skip if validated = 1 open emplty view without data ?