Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to show color based on some conditions to compare the values from my databases
I am using a web grid as my view to view those data as a table and the color will be present in a circle view I want it to return 3 different colors based on the condition but why is it only return else condition for all conditions?

What I have tried:

This is my controller
C#
public string Color()
{
    using (AEntities d = new AEntities())
    {
        Mr mr = new Mr();
       
        if (mr.A <= mr.B)
        {
            return measure.Rcolor = "#000";
        }

        if (mr.A >= mr.c)
        {
            return measure.Rcolor = "#0000FF";
        }
        else 
        {
            return measure.Rcolor = "#FFFF00";
        }
    }
}
}

public ActionResult index()
{
    Cc colo= new Cc();           
    using (AEntities d = new AEntities())
    {
        List<MViewModel> model = new List<MViewModel>();
        foreach (Mr mr in d.Mr.Take(10))
        {
            model.Add(new MViewModel
            {
                Name = mr.Name,                       
                Result = mr.Result, 
                A=mr.A,
                B-mr.B,
                C=mr.C,                      
                Rcolor = op.Color(),
            });
            ;
        }
     
        return View(model);
    }
Posted
Updated 11-Nov-21 22:40pm
v3

1 solution

Look at your code, you alway use a new object so its properties will always be as set in the constructor. You should be passing the object from somewhere else.
C#
using (AEntities d = new AEntities())
{
    Mr mr = new Mr(); // create anew object of the Mr class
    
    if (mr.A <= mr.B) // so the values of properties A and B (and C) will always be the same.
    {
    return measure.Rcolor = "#000";
    
    }
// ...
 
Share this answer
 

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