<repo_name>programinurdu/DoubleTablesASPNETMVC5<file_sep>/DoubleTablesASPNETMVC5/Scripts/Site.js
﻿// On Clicking Student Logo Image, then change the image
function ChangePicture() {
    $('#upload').click();
}

function ReadURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#image').attr('src', e.target.result);
        };
        reader.readAsDataURL(input.files[0]);
    }
}

$(document).ready(function () {
    $('#myTable2').dataTable();
});

$(document).ready(function () {
    $('#myTable').dataTable({
        "scrollY": '50vh',
        "scrollCollapse": true,
        "paging": false,
        "ajax": {
            "url": "/Student/StudentsList",
            "type": "GET",
            "datatype": "json"
        },
        "columns": [
            { "data": "StudentId" },
            { "data": "FullName" },
            { "data": "Email" },
            { "data": "GenderDesc" },
            { "data": "Mobile" },
            { "data": "Telephone" }
        ]
    });
});<file_sep>/DoubleTablesASPNETMVC5/ViewModels/Home/StudentViewModels.cs
﻿using DoubleTablesASPNETMVC5.Models.DB;
using DoubleTablesASPNETMVC5.ViewModels.Shared;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DoubleTablesASPNETMVC5.ViewModels.Home
{
    public class StudentViewModels
    {
        public List<Student> GetAllStudents()
        {
            List<Student> students = new List<Student>();

            using (StudentDB2Context db = new StudentDB2Context())
            {
                List<Student> std = db.Students.ToList();
                List<ListTypesData> ltd = db.ListTypesDatas.Where(x => x.ListTypeId == (int)ListTypes.Gender).ToList();

                students =  std.Join(ltd,
                            ss => ss.Gender,
                            ld => ld.ListTypeDataId,
                            (ss, ld) => new Student()
                            {
                                StudentId = ss.StudentId,
                                FullName = ss.FullName,
                                GenderDesc = ld.Description,
                                Email = ss.Email,
                                Mobile = ss.Mobile,
                                Telephone = ss.Telephone,
                                Notes = ss.Notes
                            }).ToList();
            }

            return students;
        }

        public void AddNewStudent(Student student)
        {
            using (StudentDB2Context db = new StudentDB2Context())
            {
                db.Students.Add(student);
                db.SaveChanges();
            }
        }

        public List<StudentsQualification> GetStudentQualificationById(int id)
        {
            List<StudentsQualification> qualifications = new List<StudentsQualification>();

            using (StudentDB2Context db = new StudentDB2Context())
            {
                qualifications = db.StudentsQualifications.Where(x => x.StudentId == id).ToList();
            }

            return qualifications;
        }

        public Student GetStudentById(int id)
        {
            Student student = new Student();

            using (StudentDB2Context db = new StudentDB2Context())
            {
                student = db.Students.Where(x => x.StudentId == id).FirstOrDefault();
            }

            return student;
        }
    }
}<file_sep>/DoubleTablesASPNETMVC5/Models/Student.cs
﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DoubleTablesASPNETMVC5.Models.DB
{
    [MetadataType(typeof(StudentMetaData))]
    public partial class Student
    {
        [Display(Name = "Gender")]
        public string GenderDesc { get; set; }
    }

    public class StudentMetaData
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public StudentMetaData()
        {
            this.StudentsQualifications = new HashSet<StudentsQualification>();
        }

        public int StudentId { get; set; }

        [Display(Name = "Full Name:")]
        public string FullName { get; set; }

        [Display(Name = "Email:")]
        public string Email { get; set; }

        [Display(Name = "Gender:")]
        [UIHint("GenderDropDownList")]
        public Nullable<int> Gender { get; set; }

        [Display(Name = "Mobile:")]
        public string Mobile { get; set; }

        [Display(Name = "Telephone:")]
        public string Telephone { get; set; }

        [Display(Name = "Notes:")]
        public string Notes { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<StudentsQualification> StudentsQualifications { get; set; }

    }

}<file_sep>/README.md
# DoubleTablesASPNETMVC5
LINQ Learning Tutorial

Discussing about LINQ and Entity Framework 6x
<file_sep>/DoubleTablesASPNETMVC5/ViewModels/Shared/SystemEnums.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DoubleTablesASPNETMVC5.ViewModels.Shared
{
    public enum ListTypes
    {
        Gender = 1,
        Qualification = 2
    }
}<file_sep>/DoubleTablesASPNETMVC5/Controllers/StudentController.cs
﻿using DoubleTablesASPNETMVC5.Models.DB;
using DoubleTablesASPNETMVC5.ViewModels.Home;
using System.Collections.Generic;
using System.Web.Mvc;

namespace DoubleTablesASPNETMVC5.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult StudentsList()
        {
            StudentViewModels svm = new StudentViewModels();
            List<Student> students = svm.GetAllStudents();

            //return View(students);
            return Json(new { data = students }, JsonRequestBehavior.AllowGet);
        }


        // New Student Screen
        public ActionResult NewStudent()
        {
            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult NewStudent(Student student)
        {
            if (ModelState.IsValid)
            {
                StudentViewModels svm = new StudentViewModels();
                svm.AddNewStudent(student);
            }

            return View();
        }

        //
        public ActionResult StudentQualification(int id=0)
        {
            if (id == 0) { return RedirectToAction("NewStudent", "Student"); }

            StudentViewModels svm = new StudentViewModels();
            List<StudentsQualification> qualifications = svm.GetStudentQualificationById(id);

            return View(qualifications);
        }
    }
}<file_sep>/DoubleTablesASPNETMVC5/Controllers/HomeController.cs
﻿using DoubleTablesASPNETMVC5.Models.DB;
using DoubleTablesASPNETMVC5.ViewModels.Home;
using System.Collections.Generic;
using System.Web.Mvc;

namespace DoubleTablesASPNETMVC5.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult TutorialsList()
        {
            return View();
        }

        // GET: Home
        public ActionResult Index()
        {
            StudentViewModels svm = new StudentViewModels();
            List<Student> students = svm.GetAllStudents();

            return View(students);
        }

        public ActionResult LoadDropDownList()
        {
            return View();
        }

        public ActionResult Edit(int id=0)
        {
            if (id == 0) { return RedirectToAction("Index", "Home"); }

            StudentViewModels svm = new StudentViewModels();
            Student student = svm.GetStudentById(id);
            LoadDataIntoDropDownList(student);

            return View("~/Views/Home/LoadDropDownList.cshtml", student);
        }

        private void LoadDataIntoDropDownList(Student student)
        {
            ViewData["Gender"] = student.Gender;
        }
    }
}<file_sep>/DoubleTablesASPNETMVC5/ViewModels/Shared/ComboBoxData.cs
﻿using DoubleTablesASPNETMVC5.Models.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DoubleTablesASPNETMVC5.ViewModels.Shared
{
    public class ComboBoxData
    {
        public static SelectList GetListData(ListTypes listType)
        {
            List<SelectListItem> list = new List<SelectListItem>();

            using (StudentDB2Context db = new StudentDB2Context())
            {
                list = db.ListTypesDatas.Where(x => x.ListTypeId == (int)listType).Select(x => new SelectListItem
                {
                    Text = x.Description,
                    Value = x.ListTypeDataId.ToString()
                }).ToList();
            }

            SelectList lst = new SelectList(list.AsEnumerable(), "Value", "Text");

            return lst;
        }
    }
}