0

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

enter image description here

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 ?

MarwanAbu
  • 181
  • 8
  • Looks like `Model.FirstOrDefault()` returns `null` – Schmittmuthelm Jul 11 '22 at 08:12
  • Action can returns null or empty list, you have to perform a null check inside your razor view before accessing `Validated` property: `FirstOrDefault()` returns null if there is no elements inside the list. – ale91 Jul 11 '22 at 08:16
  • @ale91 Validated value = 1 or 2 only not null how to check and skip the error ? – MarwanAbu Jul 11 '22 at 08:39
  • @nilsK I will check the answers and if one fix the error I will till you thank you – MarwanAbu Jul 11 '22 at 08:45
  • @MarwanAbu you have to add null check like this: `@if (Model != null && Model.FirstOrDefault() != null && Model.FirstOrDefault().program_id < 6 && Model.FirstOrDefault().Validated == 2)` – ale91 Jul 11 '22 at 08:52
  • @ale91 I tried it but still same error – MarwanAbu Jul 11 '22 at 09:01
  • Are you sure the error is in the line of code you mentioned? – ale91 Jul 11 '22 at 09:14
  • @ale91 yes see the image when I open the details and validated = 2 it open but if validated = 1 it show the error , how to open empty view if validated = 1 ? – MarwanAbu Jul 11 '22 at 09:26
  • 1
    @MarwanAbu: you missed `Model.FirstOrDefault() != null` in your code. Can you give us the model definition? – ale91 Jul 11 '22 at 09:33

0 Answers0