1

Hi guyz I have this scenario.. I have a looped table of different class(Class Name) in my view... My problem is I want to count the number of students that are present and absent.. where am I suppose to have the counting? Is it in the view? cotroller? or model? and how am I going do that? I'm quite new to this so please help me guyz.. Thanks in advance.. If you need more info please tell me..

Class Name: Star Class 
Class Adviser: James Anderson
 __________________
|  Students Status |
|__________________|
|Johny Jo | Absent |
|_________|________|
|Jenny Ye | Present|
|_________|________|

number of present: ?
number of absent: ?
2
  • Your best off doing this in the controller. What is the structure to all this though? String? or Array? etc Commented Mar 15, 2012 at 7:57
  • Im using string in my model.. Commented Mar 15, 2012 at 8:03

2 Answers 2

3

Do it in the Controller. Depending on how you have this modeled:

var count_present = db.YourModel
.Where(y => y.status.Equals("Present"))
.Count();
ViewBag.count_present = count_present;

var count_absent = db.YourModel
.Where(y => y.status.Equals("Absent"))
.Count();
ViewBag.count_absent = count_absent;

I'm using a ViewBag here to keep it simple, normally I'd use a View Model.

Sign up to request clarification or add additional context in comments.

1 Comment

I suspect the last sentence is supposed to be ViewBag.count_absent = count_absent; instead of ViewBag.count_present = count_absent; ?
1

Something like: int presentCount = attendees.Count(a => a.Status == Attendance.Present) in your controller. I'm not sure how you are storing the information, so I've just taken a punt that there is a collection of attendees that have some kind of flag to show the status.

1 Comment

+1 ok dude.. I'm gonna try that later when I go home.. :) thanks for the suggestion..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.