none
ASP.NET Web Forms to MVC 5 Code RRS feed

  • Question

  • I am trying to write the MVC5 code for an application which is in ASP.NET webforms. The method which I want to rewrite using MVC is given below. It is a login method implementation for the login page of the application. This method takes the user name and password and checks whether the person is a student or admin or alumuni and directs it to the corresponding page. I want to rewrite this logic using MVC. I have created the model classes but I am not able to figure out how to write the logic using the controller. Can someone help me with this. The code is given below:

    protected void Login(object sender, EventArgs e)
        {
            boUser loggedInUser = new boUser();
            loggedInUser = (boUser)SessionManagement.LoggedInUser;
            int urole = loggedInUser.Role;
            string Name = loggedInUser.FirstNm;
            int upid = loggedInUser.upid;
            string pEmailID = loggedInUser.PrimaryEmail;
            string uEmailID = loggedInUser.UniversityEmail;
            string uName = loggedInUser.UserName;
    
            if ((urole == Convert.ToInt32(RoleManager.Roles.CurrentStudent)) || 
                (urole == Convert.ToInt32(RoleManager.Roles.Alumni)))
             {
                TECHAPPService.ApplicationEventLogSoapClient client = new
                       TECHAPPService.ApplicationEventLogSoapClient();
    
                client.LogAppEntry("KinmanConnect", pEmailID, DateTime.Now,
    
                  Request.ServerVariables["REMOTE_ADDR"].ToString(),
    
                  Request.ServerVariables["REMOTE_HOST"].ToString(),
    
                  Request.ServerVariables["HTTP_USER_AGENT"].ToString(),
    
                  Request.ServerVariables["HTTP_REFERER"].ToString());
    
                currUser.sFName = Name;
                currUser.iRole = urole;
                currUser.upid = upid;
                currUser.uEmail = uEmailID;
                currUser.pEmail = pEmailID;
                currUser.uName = uName;
                ProspUser.PropAppYr = 0;
                currUser.iProgramID = 2;
                Response.Redirect("~/HStud/UserPages.aspx");
             }
            else if (urole == 2) //For Admin
             {
                TECHAPPService.ApplicationEventLogSoapClient client = new
                       TECHAPPService.ApplicationEventLogSoapClient();
    
                client.LogAppEntry("KinmanConnect", pEmailID, DateTime.Now,
    
                 Request.ServerVariables["REMOTE_ADDR"].ToString(),
    
                 Request.ServerVariables["REMOTE_HOST"].ToString(),
    
                 Request.ServerVariables["HTTP_USER_AGENT"].ToString(),
    
                 Request.ServerVariables["HTTP_REFERER"].ToString());
    
                currUser.sFName = Name;
                currUser.iRole = urole;
                currUser.upid = upid;
                currUser.uEmail = uEmailID;
                currUser.pEmail = pEmailID;
                currUser.uName = uName;
                ProspUser.PropAppYr = 0;
                currUser.iProgramID = 2;
                // currUser.itab = 0;
                // Response.Redirect("MainPage.aspx");
                Response.Redirect("~/HAdmin/AdminPages.aspx?tab=0");
            }
            else
                Response.Redirect("~/Default.aspx");
        }

    Friday, August 4, 2017 8:13 PM

All replies

  • Well, the controller sits between the model and the view. It's the model that has the business logic, makes calls to services, and calls the backend. It is not the controller doing it. The controller calls methods on the model to get data to populate a viewmodel and sends the viewmodel to the view.

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

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

    You know MVC is grate even the crutched up and wizards version of the ASP.NET MVC. However there is MVP, a derivative of MVC,  too that works very well with Web forms that even has 3 flavors of MVP. You ever get a chance, you should checkout MVP.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    Anyway back to MVC with the controller and the model...

    You can post to the MVC forum.

    https://forums.asp.net/1146.aspx/1?MVC

    using System.Web.Mvc;
    using MVC.Models;
    
    namespace MVC.Controllers
    {
        public class StudentsController : BaseController  
        {
            private IStudentModels studmods;
            public StudentsController(IStudentModels studentModels)
            {
                studmods = studentModels;
            }
    
            // GET: Students
            public ActionResult Index()
            {
                return View(studmods.GetStudents());
            }
            public ActionResult Details(int id = 0)
            {
                if (id == 0) return null;
    
                return View(studmods.GetStudentById(id));
            }
            public ActionResult Create()
            {
                return View(studmods.Create());
            }
    
            [HttpPost]
            public ActionResult Create(StudentViewModels.Student student)
            {
                if (ModelState.IsValid)
                {
                    studmods.Create(student);
                    return RedirectToAction("Index");
                }
    
                return View(student);
            }
            public ActionResult Edit(int id = 0)
            {
                if (id == 0) return null;
        
                return View(studmods.Edit(id));
            }
    
            [HttpPost]
            public ActionResult Edit(StudentViewModels.Student student)
            {
                if (ModelState.IsValid)
                {
                    studmods.Edit(student);
                    return RedirectToAction("Index");
                }
    
                return View(student);
            }
            public ActionResult Delete(int id = 0 )
            {
                if (id > 0)
                {
                    studmods.Delete(id);
                }
    
                return RedirectToAction("Index");
             }
        }
    }

    using System;
    using System.Collections.Generic;
    using Entities;
    using WebAPI.Controllers;
    
    namespace MVC.Models
    {
        public class StudentModels : IStudentModels
        {
            private IStudentControllerAPI studapi;
            public StudentModels(IStudentControllerAPI studentControllerApi)
            {
                studapi = studentControllerApi;
            }
    
            public StudentViewModels GetStudents()
            {
                var dtos = studapi.GetStudents();
               
                var vm = new StudentViewModels {Students = new List<StudentViewModels.Student>()};
    
                foreach (var dto in dtos)
                {
                    var student = new StudentViewModels.Student
                    {
                        StudentID = dto.StudentID,
                        LastName = dto.LastName,
                        FirstName = dto.FirstName,
                        EnrollmentDate = dto.EnrollmentDate
                    };
    
                    vm.Students.Add(student);
                }
    
                return vm;
            }
    
            public StudentViewModels.Student GetStudentById(int id)
            {
                var dto = studapi.GetStudentById(id);
    
                var student = new StudentViewModels.Student
                {
                    StudentID = dto.StudentID,
                    FirstName = dto.FirstName,
                    LastName = dto.LastName,
                    EnrollmentDate = dto.EnrollmentDate,
                    EnrollsandCourses = new List<EnrollandCourseViewModel.EnrollandCourse>()
                };
    
                foreach (var dtoec in dto.EnrollsandCourses)
                {
                    var ec = new EnrollandCourseViewModel.EnrollandCourse
                    {
                        Credits = dtoec.Credits,
                        Grade = dtoec.Grade,
                        Title = dtoec.Title
                    };
    
                    student.EnrollsandCourses.Add(ec);
                }
    
                return student;
            }
    
            public StudentViewModels.Student Create()
            {
                var student = new StudentViewModels.Student();
    
                return student;
            }
    
            public void Create(StudentViewModels.Student student)
            {
                var dto = new DTOStudent
                {
                    StudentID = student.StudentID,
                    FirstName = student.FirstName,
                    LastName = student.LastName,
                    EnrollmentDate = student.EnrollmentDate
                };
    
                studapi.CreateStudent(dto);
            }
    
            public StudentViewModels.Student Edit(int id)
            {
                var dto = studapi.GetStudentById(id);
    
                var student = new StudentViewModels.Student
                {
                    StudentID = dto.StudentID,
                    FirstName = dto.FirstName,
                    LastName = dto.LastName,
                    EnrollmentDate = dto.EnrollmentDate
                };
    
                return student;
            }
    
            public void Edit(StudentViewModels.Student student)
            {
                var dto = new DTOStudent
                {
                    StudentID = student.StudentID,
                    FirstName = student.FirstName,
                    LastName = student.LastName,
                    EnrollmentDate = student.EnrollmentDate
                };
    
                studapi.UpdateStudent(dto);
            }
    
            public void Delete(int id)
            {
                studapi.DeleteStudent(id);
            }
        }
    }

    Friday, August 4, 2017 9:32 PM