Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. i have first page name"Homepage.cshtml", second page, "UnitDetails.cshtml". My model name is "DetailBundle" and controller is "Home"

my table databases are table "Tester" and "Parameter" where the foreign key is "ParameterID"

in Homepage, i have table which displays all tester name (from db.tester). and if i click the tester name, it will direct to UnitDetails and it will bring the TesterID data. In database, i have specify every tester name with its condition whether good/bad. (note that good/bad is in db.Parameter)

so i want my table to colour according to good:green and Bad:red.

i tried using the query but when i run it state exeption unhandled.

What I have tried:

this is my controller:

public ActionResult Homepage()
       {
           List<DetailBundle> query = (from i in db.Testers
                        from a in db.Parameters
                        .Where(a => a.ParameterID == i.ParameterID)

                        select new DetailBundle
                        {
                            TesterID = i.TesterID,
                            TesterName = i.TesterName,
                            Param_desc = a.Param_desc

                        }).Distinct().OrderBy(i => i.TesterID).ToList();
           ViewBag.TesterUnit = query;
           return View();
       }


this is my Homepage:
@if (Tester.Contains(ViewBag.TesterUnit[i].TesterName))
                    {
                        if (ViewBag.TesterUnit[i].Param_desc == "Good")
                        {
                            <td class="custom" style="text-align:center; color:red">
                                @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                            </td>
                        }
                        else if (ViewBag.TesterUnit[i].Param_desc == "Bad")
                        {
                            <td class="custom" style="text-align:center; color:green">
                                @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                            </td>
                        }
                        else
                        {
                            <td class="custom" style="text-align:center; color:white">
                                @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                            </td>
                        }
                        i++;

                        if (Tester.Contains(ViewBag.TesterUnit[i].TesterName))   //display only if it is in Desktop List.
                        {
                            if (ViewBag.TesterUnit[i].Param_desc == "Good")
                            {
                                <td class="custom" style="text-align:center; color:red">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                            else if (ViewBag.TesterUnit[i].Param_desc == "Bad")
                            {
                                <td class="custom" style="text-align:center; color:green">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                            else
                            {
                                <td class="custom" style="text-align:center; color:white">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                        }
                        i++;
                        if (Tester.Contains(ViewBag.TesterUnit[i].TesterName))   //display only if it is in Desktop List.
                        {
                            if (ViewBag.TesterUnit[i].Param_desc == "Good")
                            {
                                <td class="custom" style="text-align:center; color:red">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                            else if (ViewBag.TesterUnit[i].Param_desc == "Bad")
                            {
                                <td class="custom" style="text-align:center; color:green">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                            else
                            {
                                <td class="custom" style="text-align:center; color:white">
                                    @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
                                </td>
                            }
                        }
                    }
Posted
Updated 8-Jul-20 6:47am

1 solution

Try something like this:
Razor
@for (int i = 0; i < ViewBag.TesterUnit.Count; i++)
{
    int recordsInRow = 3;
    
    <tr>
    @while (recordsInRow > 0 && i < ViewBag.TesterUnit.Count)
    {
        if (Tester.Contains(ViewBag.TesterUnit[i].TesterName))
        {
            recordsInRow--;
            
            string cssClass;
            switch (ViewBag.TesterUnit[i].Param_desc)
            {
                case "Good":
                    cssClass = "custom text-success";
                    break;
                case "Bad":
                    cssClass = "custom text-danger";
                    break;
                default:
                    cssClass = "custom text-light";
                    break;
            }
            
            <td class="@cssClass">
                @Html.ActionLink((string)ViewBag.TesterUnit[i].TesterName, "UnitDetails", new { id = (object)ViewBag.TesterUnit[i].TesterID }, new { target = "_blank" })
            </td>
        }
        
        i++;
    }
    </tr>
}
This uses the Bootstrap colour utility classes: Colors · Bootstrap v4.5[^]
 
Share this answer
 
Comments
dlnzki 8-Jul-20 13:23pm    
the same TesterName appears in 3 column of one row. and the colour of the font is not changing also. i thought it was maybe because of the HtmlLink that i used,so i change the text-success to bg-success. but still got no difference.
dlnzki 8-Jul-20 13:26pm    
the same TesterName appears in 3 column of 1 row. the color of the tester name also not change. i thought maybe it is because of the htmlLink since the default colour is blue. so i change the text-success to bg-success. but nothing happen. please help. thank you
dlnzki 8-Jul-20 13:29pm    
tried to comment for third time.
the output of the same tester name appear in 3 column of the same row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900