Home > Back-end >  How can I display data in the MVC VIEW based on condition?
How can I display data in the MVC VIEW based on condition?

Time:07-06

I need to show the view page based on condition if program_id = 6 then show set of columns

and if program_id < 6

then show different set of columns

This is the controller :

public ActionResult Details(int id , int programId, int custId)
    {
      
        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).ToList();
        return View(results);
    }

This is view code :

@model IEnumerable<warehouse.Models.RESULT>

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}

<img style="margin-left:250px;" src="~/images/weblogo.png" />
<div style="margin-left:50px;"> @Html.ActionLink("Back to List", "Indexs", "HospitalSamples") </div>

<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.accepted_BG)</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.accepted_BG)
    </td>
        <td>
            @Html.DisplayFor(modelItem => item.Machine.Machine_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.performance.name)
        </td>
      
    </tr>
}

</table>

and this is the ACTIONLINK call the view:

 @Html.ActionLink("Report", "Details", new { id = item.sample_id, programId = item.program_id, custId = item.custid }, new { @class = "btn btn-danger" }) 

I need to show different columns in the view based on the following condition:

    If program_id = 6 then 
    {   
    
        <img style="margin-left:250px;" src="~/images/weblogo.png" />
        <div style="margin-left:50px;"> @Html.ActionLink("Back to List", "Indexs", "HospitalSamples") </div>
        
        <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>
        
    }
    
    else 
{

<img style="margin-left:250px;" src="~/images/weblogo.png" />
<div style="margin-left:50px;"> @Html.ActionLink("Back to List", "Indexs", "HospitalSamples") </div>

<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>


}

I tried to do it in view but its not working how can I do it the condition in controller or view ?

CodePudding user response:

You can not use If program_id = 6 then in cshtml

you must use @If (program_id == 6) .

  • Related