locked
Select Student Record Based on Foreach loop RRS feed

  • Question

  • User210895818 posted

    I am working on MVC. I need to selecr list of students based on enrolled department wise and display them on different divs departmentalize, based on enrolled department in view. My database sample Schemas is as follow

    Student Table

    ID, Name

    Enrollment Table

    ID
    Depart_id
    Stdref_id

    Department Table

    ID
    DepartName

    I am trying something like this:

    public ActionResult NewStudentFeeStatus()
        {
            IEnumerable<tblStdDetail> mdl = null;
            var departments= (from d in db.tblDepartments select d).ToList();
            var query = "";
            List<StdListModel> model = new List<StdListModel>();
            foreach (var item in departments)
            {
                query = (from p in db.tblStdDetails
                         join e in db.tblStdEnrollments on p.ID equals 
                         e.StdReg_ref_id
                         where e.Depart_ref_id == @item.ID
                         select p).ToList();
    
            }
            return View();
        }

    I need a direction or sample code to follow for required output. I don't know what to do next and how to manage it in view. Kindly help me if you have any idea.

    Thursday, August 24, 2017 8:47 AM

All replies

  • User1771544211 posted

    Hi Ridzi,

    I need a direction or sample code to follow for required output. I don't know what to do next and how to manage it in view. Kindly help me if you have any idea.

    Your query should return a list of StdDetails, so if you want to display the content of the StdDetails in your view, you can try the following code.

    return View(query);

    @model IEnumerable<Project.Models.StdDetails>
    
    @{
        ViewBag.Title = "StdDetails
    ";
    }
    
    
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ID )
            </th>
            <th>
                @Html.DisplayNameFor(model => model.XXX)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.XXX2)
            </th>
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.XXX)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.XXX2)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID}) |
                    @Html.ActionLink("Details", "Details", new { id = item.ID}) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.ID})
                </td>
            </tr>
        }
    
    </table>

    Best Regards,

    Jean

    Friday, August 25, 2017 5:59 AM
  • User1120430333 posted

    You are breaking SoC and MVC principles, because you have the controller doing direct access with the DB.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    http://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    I am using services. Actually,  the MVC model methods are calling WebAPI controller, that are calling a WCF Web service and it has the Repository and DAO patterns at the DAL.

     http://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

    I bring this up because how you shape the data to be shown for a given viewmodel should have been shaped at the DAL using DTO(s).

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://juristr.com/blog/2012/10/lessions-learned-dont-expose-ef-entities-to-the-client-directly/

    GetStudentById() is where I shape the data for the viewmodel.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Entity;
    using Entities;
    using DAL.Model;
    
    namespace DAL.DAO
    {
        public class DAOStudent : IDAOStudent
        {
            public DTOStudent GetStudentById(Int32 id)
            {
                var dto = new DTOStudent();
                using (var context = new CUDataEntities())
                {
                    var student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
    
                    if (student != null)
                    {
                        dto.StudentID = student.StudentID;
                        dto.FirstName = student.FirstName;
                        dto.LastName = student.LastName;
                        dto.EnrollmentDate = student.EnrollmentDate;
    
                        var enrolllments =  new DAOEnrollment().GetEntrollmentsByStudentId(id).ToList();
                        var courses = new DAOCourse().GetCoursesByStudentCourseId(student.StudentID).ToList();
    
                        dto.EnrollsandCourses = (from a in enrolllments
                                      join b in courses on a.CourseID equals b.CourseID
                        select new  DTOEnrollandCourse()
                         { Title = b.Title, Credits = b.Credits, Grade = a.Grade }).ToList();
                    }
                }
    
                return dto;
            }
            public void CreateStudent(DTOStudent dto)
            {
                using (var context = new CUDataEntities())
                {
                    var student = new Student
                    {
                        FirstName = dto.FirstName,
                        LastName = dto.LastName,
                        EnrollmentDate = dto.EnrollmentDate
                    };
    
                    context.Students.Add(student);
                    context.SaveChanges();
                }
            }
    
            public void DeleteStudent(int id)
            {
                Student student;
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
                }
    
                using (var newContext = new CUDataEntities())
                {
                    newContext.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                    newContext.SaveChanges();
                }
            }
    
            public List<DTOStudent> GetStudents()
            {
                var dtos = new List<DTOStudent>();
    
                using (var context = new CUDataEntities())
                {
                    var students = context.Students.ToList();
    
                    foreach(var stud in students)
                    {
                        var dto = new DTOStudent
                        {
                            StudentID = stud.StudentID,
                            FirstName = stud.FirstName,
                            LastName = stud.LastName,
                            EnrollmentDate = stud.EnrollmentDate
                        };
    
                        dtos.Add(dto);
                    }
                }
    
                return dtos;
            }
    
            public void UpdateStudent(DTOStudent dto)
            {
                var student = new Student();
    
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
                }
    
                if (student != null)
                {
                    student.FirstName = dto.FirstName;
                    student.LastName = dto.LastName;
                    student.EnrollmentDate = dto.EnrollmentDate;
                } 
    
                using (var dbcontext = new CUDataEntities())
                {
                    if (student != null)
                    {
                        dbcontext.Entry(student).State = EntityState.Modified;
                        dbcontext.SaveChanges();
                    }
                }
            }
        }
    }
    

    Saturday, August 26, 2017 7:59 PM