Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to calculate the average marks for a student

What I have tried:

this is my viewmodel
public class StudentCoursesViewModel 

    {
        public uniscoreEntities db = new uniscoreEntities();
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Code { get; set; }
        public string CourseName { get; set; }
        public int Credit { get; set; }
        public int Mark { get; set; }
        public string Grade { get; set; }
        

        
    }


this is my controller
namespace UniScore.Controllers
{
    public class StudentCoursesController : Controller
    {
        // GET: StudentCourses
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index(int? id)
        {

            uniscoreEntities db = new uniscoreEntities();
            Student student = db.Students.Find(id);


            List<StudentCoursesViewModel> StudentCoursesList = new List<StudentCoursesViewModel>();
            // var coursesList = db.Courses.ToList();
            // var StudentCourses = db.Student_Course.ToList();
            Student_Course injec = new Student_Course();

            var StudentCourses = db.Student_Course.Where(b => b.Student_id == id);




            foreach (var item in StudentCourses)
            {
                StudentCoursesViewModel objele = new StudentCoursesViewModel();
                Student studento = db.Students.Find(item.Student_id);
                objele.FirstName = studento.FirstName;
                objele.LastName = studento.LastName;
                Course course = db.Courses.Find(item.Course_id);
                objele.CourseName = course.CourseName;
                objele.Code = course.Code;
                objele.Credit = course.Credit;
               // Student_Course testy = db.Student_Courses.Find(item.Marks);
                objele.Grade = item.Grade;
                objele.Mark = item.Mark;


                StudentCoursesList.Add(objele);


            }
            return View(StudentCoursesList);
        }
    }
}



this is my view
@model  IEnumerable<UniScore.ViewModels.StudentCoursesViewModel>

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

<h2>Details</h2>

<div>
    <h4>Student</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>

        <dd>

        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>

        <dd>

        </dd>
    </dl>
</div>



<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CourseName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Code)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Credit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Mark)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Grade)
        </th>
    </tr>


    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CourseName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Code)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Credit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mark)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Grade)
            </td>
        </tr>
    }

</table>

<p>
    
        AVERAGE MARKS:
    
</p>




<p>
    @Html.ActionLink("Back to List", "Index", "Students")
</p>
Posted
Updated 12-Mar-17 2:17am

1 solution

if the model in the view is representing all the courses of one student, then you can just sum the total marks and divide it by number of courses to get the average of him:

C#
<p>    
AVERAGE MARKS: @(Model.Sum(x=>x.Mark)/Model.Count()) 
</p>


and your ViewModel is not well defined, technically your ViewModel should have all the properties that needs to be used in View, so what would be better is to include a property of Average in your ViewModel and your view should be just displaying that, not calculating it in view.

A better ViewModel would be something like:

C#
public class StudentCoursesViewModel 
{
    public List<Course> Courses {get;set;}
    public AverageMarks 
    {
       get { return (Course.Sum()/Courses.Count()); }
          
    }
        
}

public class Course
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int Code { get; set; }
   public string CourseName { get; set; }
   public int Credit { get; set; }
   public int Mark { get; set; }
   public string Grade { get; set; }
}


Hope it helps!
 
Share this answer
 
v8
Comments
Afzaal Ahmad Zeeshan 12-Mar-17 8:21am    
5ed.
Member 13053943 12-Mar-17 8:34am    
please which type of Model are you talking about.should i copy and paste what you suggested in my code?please explain your answer.I'm new to this asp.net mvc 5
Ehsan Sajjad 12-Mar-17 8:41am    
for the time being you can write the above code in view to keep going, but viewmodel should be the place to do this.
Member 13053943 12-Mar-17 9:08am    
bro,it didn't work,the view just displayed the code you sent.i'm very confused here
Ehsan Sajjad 12-Mar-17 13:14pm    
put
@
before the code, i missed that

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