0% found this document useful (0 votes)
15 views18 pages

mvc controller

The document contains the implementation of two controllers, HomeController and AdminController, for a web application related to RG Defence Academy. The HomeController manages actions such as displaying feedback, handling contact submissions, and managing testimonials, while the AdminController oversees administrative tasks including managing student admissions, faculty, and gallery items. Both controllers utilize a database context to interact with data models and provide feedback through alert messages upon various actions.

Uploaded by

SK Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views18 pages

mvc controller

The document contains the implementation of two controllers, HomeController and AdminController, for a web application related to RG Defence Academy. The HomeController manages actions such as displaying feedback, handling contact submissions, and managing testimonials, while the AdminController oversees administrative tasks including managing student admissions, faculty, and gallery items. Both controllers utilize a database context to interact with data models and provide feedback through alert messages upon various actions.

Uploaded by

SK Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RGDefenceAcademy.Models;

namespace RGDefenceAcademy.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

RGAcdemyDBEntities db = new RGAcdemyDBEntities();

public ActionResult Index()


{
List<Tbl_Feedback> TblFeeds = db.Tbl_Feedback.OrderByDescending(t =>
t.FeedId).Take(9).ToList();
return View(TblFeeds);
}
public ActionResult AboutUs()
{
return View();
}
public ActionResult Facility()
{
return View(db.Tbl_Faculity.ToList());
}
public ActionResult ContactUs()
{
return View();
}
[HttpPost]
public ActionResult ContactUs(Tbl_Contact con)
{
try
{
db.Tbl_Contact.Add(con);
db.SaveChanges();
Response.Write("<script>alert('Contact Save
SuccessFully')</script>");
}
catch (Exception)
{
Response.Write("<script>alert('Something Went Wrong')</script>");
}

return View();
}
public ActionResult AdmissionForm()
{
return View();
}
public ActionResult LogIn()
{
return View();
}
[HttpPost]
public ActionResult LogIn(string username, string pword)
{
if (username == "RGAdmin" && pword == "rg@123")
{
Session["username"] = username;
Response.Write("<script>alert('Welcome to Admin Zone
');window.location.href='/Admin/Index'</script>");
}
else
{
Response.Write("<script>alert('Invalid AdminId or
Password')</script>");
}
return View();
}

public ActionResult Testimonial()


{
List<Tbl_Feedback> TblFeeds = db.Tbl_Feedback.OrderByDescending(t =>
t.FeedId).Take(9).ToList();
return View(TblFeeds);
}
[HttpPost]
public ActionResult Testimonial(Tbl_Feedback model)
{
try
{
HttpPostedFileBase file = Request.Files["ProfilePic"];
if(file.FileName != "")
{
model.ProfilePic = file.FileName;
file.SaveAs(Server.MapPath("../Content/FeedPic/" +
file.FileName));
db.Tbl_Feedback.Add(model);
db.SaveChanges();
Response.Write("<script>alert('Your Feedback Submitted
Successfully');window.location.href='/Home/Testimonial'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your
profile')</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('something went wrong')</script>");
}
return View();
}
public ActionResult OurGallery()
{
List<Tbl_Gallery> TblGallery = db.Tbl_Gallery.OrderByDescending(t =>
t.GalleryId).Take(1000).ToList();
return View(TblGallery);
}
}
}

Admin Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RGDefenceAcademy.Models;

namespace RGDefenceAcademy.Controllers
{
public class AdminController : Controller
{
//
// GET: /Admin/

RGAcdemyDBEntities db = new RGAcdemyDBEntities();

public ActionResult Index()


{
if (Session["username"] != null)
{

}
else
{

Response.Write("<script>alert('Logout');window.location.href='/Home/LogIn'</script>");
}
return View();
}

public ActionResult ContactList()


{
//List<Tbl_Contact> lst = null;
//if (Session["aid"] != null)
//{
// lst = db.Tbl_Contact.ToList();
//}
//else
//{
//
Response.Write("<script>alert('Logout');window.location.href='/Home/Login'</script>");
//}
return View(db.Tbl_Contact.ToList());
}

public void DeleteContact(int ContactID)


{
try
{
Tbl_Contact model = db.Tbl_Contact.SingleOrDefault(a => a.ContactID == ContactID);
db.Tbl_Contact.Remove(model);
db.SaveChanges();
Response.Write("<script>alert('Student Information Deleted
Successfully');window.location.href='/Admin/ContactList'</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/ContactList'</script>");
}
}

public ActionResult StudentAdmission()


{
if (Session["username"] != null)
{

}
else
{

Response.Write("<script>alert('Logout');window.location.href='/Home/Login'</script>");
}
return View();
}
[HttpPost]
public ActionResult StudentAdmission(Tbl_Admission model, string submit)
{
try
{

HttpPostedFileBase file = Request.Files["StudentPic"];


if (file.FileName != "")
{
model.StudentPic = file.FileName;
file.SaveAs(Server.MapPath("../Content/studentimg/" + file.FileName));
model.Adddate = DateTime.Now;
db.Tbl_Admission.Add(model);
db.SaveChanges();
Response.Write("<script>alert('Student Admission Successfully
Completed');window.location.href='/Admin/StudentList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your profile')</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('something went wrong')</script>");
}
return View();
}

public ActionResult UpdateStudentAdmission(int StudentId)


{

Tbl_Admission model = db.Tbl_Admission.SingleOrDefault(a => a.StudentId ==


StudentId);

return View(model);
}
[HttpPost]
public void UpdateStudentAdmission(Tbl_Admission model)
{

try
{
Tbl_Admission up = db.Tbl_Admission.SingleOrDefault(a => a.StudentId ==
model.StudentId);

HttpPostedFileBase file = Request.Files["StudentPic"];


if (file.FileName != "")
{
up.StudentPic = file.FileName;
file.SaveAs(Server.MapPath("../Content/studentimg/" + file.FileName));

up.StudentName = model.StudentName;
up.FatherName = model.FatherName;
up.MotherName = model.MotherName;
up.Class = model.Class;
up.DateOfBirth = model.DateOfBirth;
up.Gender = model.Gender;
up.MobileNo = model.MobileNo;
up.Address = model.Address;
db.SaveChanges();
Response.Write("<script>alert('Student Information Updated
Successfully');window.location.href='/Admin/StudentList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your
profile');window.location.href='/Admin/UpdateStudentAdmission'</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/UpdateStudentAdmission'</script>");
}
}
public ActionResult StudentList()
{
return View(db.Tbl_Admission.ToList());
}

public void DeleteStudent(int StudentId)


{
try
{
Tbl_Admission model = db.Tbl_Admission.SingleOrDefault(a => a.StudentId ==
StudentId);
db.Tbl_Admission.Remove(model);
db.SaveChanges();
Response.Write("<script>alert('Student Information Deleted
Successfully');window.location.href='/Admin/StudentList'</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/StudentList'</script>");
}

public ActionResult Faculty()


{
return View();
}
[HttpPost]
public ActionResult Faculty(Tbl_Faculity model, string submit)
{
try
{
HttpPostedFileBase file = Request.Files["FaculityPic"];
if (file.FileName != "")
{
model.FaculityPic = file.FileName;
file.SaveAs(Server.MapPath("../Content/FacultyPic/" + file.FileName));
model.AddDate = DateTime.Now;
db.Tbl_Faculity.Add(model);
db.SaveChanges();
Response.Write("<script>alert('Student Admission Successfully
Completed');window.location.href='/Admin/facultyList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your profile')</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('something went wrong')</script>");
}
return View();
}

public ActionResult facultyList()


{
return View(db.Tbl_Faculity.ToList());
}
public ActionResult UpdateFaculty(int FaculityId)
{

Tbl_Faculity model = db.Tbl_Faculity.SingleOrDefault(a => a.FaculityId == FaculityId);

return View(model);
}
[HttpPost]
public void UpdateFaculty(Tbl_Faculity model)
{

try
{
Tbl_Faculity up = db.Tbl_Faculity.SingleOrDefault(a => a.FaculityId ==
model.FaculityId);

HttpPostedFileBase file = Request.Files["FaculityPic"];


if (file.FileName != "")
{
up.FaculityPic = file.FileName;
file.SaveAs(Server.MapPath("../Content/FacultyPic/" + file.FileName));

up.FaculityName = model.FaculityName;
up.Degination = model.Degination;
up.Qualifaction = model.Qualifaction;
up.MobileNo = model.MobileNo;
up.Gender = model.Gender;
up.Address = model.Address;
db.SaveChanges();
Response.Write("<script>alert('Faculty Information Updated
Successfully');window.location.href='/Admin/facultyList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your
profile');window.location.href='/Admin/UpdateStudentAdmission'</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('Some thing went
wrong');window.location.href='/Admin/UpdateStudentAdmission'</script>");
}
}

public void DeleteFaculty(int FaculityId)


{
try
{
Tbl_Faculity model = db.Tbl_Faculity.SingleOrDefault(a => a.FaculityId == FaculityId);
db.Tbl_Faculity.Remove(model);
db.SaveChanges();
Response.Write("<script>alert('Faculty Information Deleted
Successfully');window.location.href='/Admin/facultyList'</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/facultyList'</script>");
}
}

public ActionResult TestimonialList()


{
return View(db.Tbl_Feedback.ToList());
}

public void DeleteTestimonial(int FeedId)


{
try
{
Tbl_Feedback model = db.Tbl_Feedback.SingleOrDefault(a => a.FeedId == FeedId);
db.Tbl_Feedback.Remove(model);
db.SaveChanges();
Response.Write("<script>alert('Faculty Information Deleted
Successfully');window.location.href='/Admin/TestimonialList'</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/TestimonialList'</script>");
}
}
public ActionResult Gallery()
{
return View();
}
[HttpPost]
public ActionResult Gallery(Tbl_Gallery model)
{
try
{
HttpPostedFileBase file = Request.Files["PicName"];
if (file.FileName != "")
{
model.PicName = file.FileName;
file.SaveAs(Server.MapPath("../Content/Gallery/" + file.FileName));
db.Tbl_Gallery.Add(model);
db.SaveChanges();
Response.Write("<script>alert('Image Added
Successfully');window.location.href='/Admin/GalleryList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your profile')</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/GalleryList'</script>");
}
return View();
}

public ActionResult GalleryList()


{
return View(db.Tbl_Gallery.ToList());
}

public ActionResult UpdateGallery(int GalleryId)


{
Tbl_Gallery model = db.Tbl_Gallery.SingleOrDefault(a => a.GalleryId == GalleryId);

return View(model);
}
[HttpPost]
public ActionResult UpdateGallery(Tbl_Gallery model,int GalleryId)
{
try
{
Tbl_Gallery up = db.Tbl_Gallery.SingleOrDefault(a => a.GalleryId == model.GalleryId);

HttpPostedFileBase file = Request.Files["PicName"];


if (file.FileName != "")
{
up.PicName = file.FileName;
file.SaveAs(Server.MapPath("../Content/FacultyPic/" + file.FileName));
db.SaveChanges();
Response.Write("<script>alert('Gallery Updated
Successfully');window.location.href='/Admin/GalleryList'</script>");
}
else
{
Response.Write("<script>alert('Kindly upload your
profile');window.location.href='/Admin/UpdateGallery'</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('Some thing went
wrong');window.location.href='/Admin/UpdateGallery'</script>");
}
return View(model);
}
public void DeleteGallery(int GalleryId)
{
try
{
Tbl_Gallery model = db.Tbl_Gallery.SingleOrDefault(a => a.GalleryId == GalleryId);
db.Tbl_Gallery.Remove(model);
db.SaveChanges();
Response.Write("<script>alert('Image Deleted
Successfully');window.location.href='/Admin/GalleryList'</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('some thing went
wrong');window.location.href='/Admin/GalleryList'</script>");
}
}
public void LogOut()
{
Session.Abandon();
Response.Write("<script>alert('LogOut');window.location.href='/Home/LogIn'</script>");
}
}
}

Login Page Home


<form action="/Home/LogIn" method="post">
<div class="row g-3">
<div class="col-12">
<div class="form-floating">
<input type="text" class="form-control" name="username"
placeholder="Subject">
<label for="subject">Mobile Number</label>
</div>
</div>
<div class="col-12">
<div class="form-floating">
<input type="password" class="form-control" name="pword"
placeholder="Password">
<label for="message">Password</label>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary w-100 py-3" type="submit">LogIn</button>
</div>
</div>
</form>

You might also like