Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Which solution is better when using mvc to get average,set backgound color in a grid?
I use mvc,for example,in controller I return Json(list<student>) and show the student imfomation in a grid in view,Now I want to show the grid with calculating the average of point in girl student in Class 1,and mark the td background red when the boy student's point<60;(may be more Complex logic).Now I have to solution:
1.in the view get the json data,and change the data from the grid to be list<student> model and then calcult,do not to search the database,but seems difficult
2. get the calculate result from server(means controller),but it need to search the database
Posted
Updated 18-Apr-13 2:55am
v2

1 solution

Hi,

I hope I understand the question correctly.

Normally you should minimize the code-usage in a view. This is because a view is normally not compiled but just interpreted on runtime.

So I would suggest you, that you extend your student model with an enum, which contains the state of the average points of this student. Now on the view simply use this state for example to form a css class and style the td in a .css stylesheet (please DON'T use inline-styles, because if someone want to change a style later on, it is nearly impossible to do so with inline styles).

Here is a small example:

Model-Data:
[
  { name: "student 1", points: 40, state: TooLow },
  { name: "student 2", points: 60, state: Ok },
  { name: "student 3", points: 100, state: Excellent }
]


View:
HTML
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Points</th>
    </tr>
  </thead>
  <tbody>
    @foreach (var student in Model)
    {
      <tr>
        <td>@student.name</td>
        <td class="@student.state">@student.points</td>
      </tr>
    }
  </tbody>
</table>


Css:
CSS
table td.TooLow {
  background-color: #f00;
}
table td.Ok {
  background-color: #ff0;
}
table td.Excellent {
  background-color: #0f0;
}


Hope this helps :)

Best regards and happy coding,
Chris
 
Share this answer
 
v2

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