本文介绍了如何计算ASP.NET mvc5中学生的分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一个学生的平均分数



我尝试过:



这是我的viewmodel

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>

推荐答案

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





并且您的ViewModel定义不明确,从技术上讲,您的ViewModel应该具有需要在View中使用的所有属性,因此最好在ViewModel中包含Average属性,并且您的视图应该只是显示,不计算在视野中。



更好的ViewModel类似于:





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:

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!


这篇关于如何计算ASP.NET mvc5中学生的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:09