How to Use Session Object in asp.net mvc 4
CRUD OPERATIONS on LIST Object IN ASP.NET MVC 4
2) Create a Equivalent class for this Profile Table in Model folder
public class Profile
{
public int ID{get;set;}
public String Name{get;set;}
public String Mobile{get;set;}
public String EmailID{get;set;}
public DateTime? doj{get;set;}
}
3) Add a controller
right click on Controller Folder->Click on Add Controller name it as ProfileController Select template as
4) Create a List of Profile object
List<Profile> profiles = new List<Profile>()
{
new Profile{Name="Pete",Mobile="2023034004",EmailID="pete@yahoo.com",doj=new DateTime(2013,12,12)},
new Profile{Name="Hans",Mobile="4073035005",EmailID="hans@gmail.com",doj=new DateTime(2000,1,22)},
};
select Model class as Profile, Scaffolding as List
add following code in Index Method.
public ActionResult Index()
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
else
Session["Profiles"]=profiles;
}
5) In Create method
add View select Model class as Profile, Scaffolding as Create
2 Create methods will be created
1. Create will allow user to input data
2. To Post user entered data to server
public ActionResult Create()
{
//right click on this method Click on "Add View"
//Select Model class as Profile
//Scaffolding as Create
return View();
}
}
6) in Create(FormCollection) method
Change from Create(FormCollection) to Create(Profile p) and then
add following code.
//This method is called whenever user posts the data
[HttpPost]
public ActionResult Create(Profile p)//FormCollection collection)
{
try
{
[HttpPost]
public ActionResult Create(Profile p)//FormCollection collection)
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
profiles.add(p);
Session["Profiles"]=profiles;
}
catch
{
return View();
}
}
catch
{
return View();
}
}
7. Add Edit View
right click on Edit Method View Select Model class as Profile, Scaffolding as Edit
public ViewResult Edit(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
Profile profile = profiles.find(id);
return View(profile);
}
//Once user updates the record clicks on Save button, the information will be saved to database and then return to Index Page, which again queries all records from the database.
//
// POST: /Profile/Edit/5
[HttpPost]
public ActionResult Edit(int id, Profile p)
{
try
{
// TODO: Add update logic here
}
//Once user updates the record clicks on Save button, the information will be saved to database and then return to Index Page, which again queries all records from the database.
//
// POST: /Profile/Edit/5
[HttpPost]
public ActionResult Edit(int id, Profile p)
{
try
{
// TODO: Add update logic here
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
Profile profile = profiles.find(id);
profile.Name = p.Name;
profile.EmailID = p.EMailID;
profile.doj = p.doj;
Session["Profiles"]=profiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class ProfileController : Controller
{
List<Profile> profiles = new List<Profile>()
{
new Profile{Name="Pete",Mobile="2023034004",EmailID="pete@yahoo.com",doj=new DateTime(2013,12,12), ID=101},
new Profile{Name="Hans",Mobile="4073035005",EmailID="hans@gmail.com",doj=new DateTime(2000,1,22), ID=102}
};
//
// GET: /Profile/
public ActionResult Index()
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
else
Session["Profiles"]=profiles;
return View(profiles);
}
public ActionResult Create()
{
//right click on this method Click on "Add View"
//Select Model class as Profile
//Scaffolding as Create
return View();
}
[HttpPost]
public ActionResult Create(Profile p)//FormCollection collection)
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
p.ID = GetNextProfileID();
profiles.Add(p);
Session["Profiles"]=profiles;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
private int GetNextProfileID()
{
if (Session["Profiles"] != null)
{
profiles = Session["Profiles"] as List<Profile>;
}
return (from p in profiles
select p.ID).Max() + 1;
}
public ViewResult Edit(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
[HttpPost]
public ActionResult Edit(int id, Profile p)
{
try
{
// TODO: Add update logic here
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(pp => pp.ID == id);
profile.Name = p.Name;
profile.Mobile = p.Mobile;
profile.EmailID = p.EmailID;
profile.doj = p.doj;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Details(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
public ActionResult Delete(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
[HttpPost]
public ActionResult Delete(int id, Profile p)//FormCollection collection
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(pp => pp.ID == id);
profiles.Remove(profile);
Session["Profiles"]=profiles;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Details(int id)
{
}
catch
{
return View();
}
}
7. Add Details View
right click on Details Method View Select Model class as Profile, Scaffolding as Details
public ActionResult Details(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
Profile profile = profiles.find(id);
return View(profile);
}
public ActionResult Delete(int id)
{
}
8. Add Delete View
right click on Delete Method View Select Model class as Profile, Scaffolding as Delete
2 Delete Methods will be displayed
1. Delete method displays record details
2.Second Delete method Posts delete record details
public ActionResult Delete(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
Profile profile = profiles.find(id);
return View(profile);
}
[HttpPost]
public ActionResult Delete(int id, Profile p)//FormCollection collection
{
try
{
}
//Change FormCollection to Profile object
[HttpPost]
public ActionResult Delete(int id, Profile p)//FormCollection collection
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>();
}
Profile profile = profiles.find(id);
profiles.Remove(profile);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
catch
{
return View();
}
}
Here is the complete code of ProfileController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class ProfileController : Controller
{
List<Profile> profiles = new List<Profile>()
{
new Profile{Name="Pete",Mobile="2023034004",EmailID="pete@yahoo.com",doj=new DateTime(2013,12,12), ID=101},
new Profile{Name="Hans",Mobile="4073035005",EmailID="hans@gmail.com",doj=new DateTime(2000,1,22), ID=102}
};
//
// GET: /Profile/
public ActionResult Index()
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
else
Session["Profiles"]=profiles;
return View(profiles);
}
public ActionResult Create()
{
//right click on this method Click on "Add View"
//Select Model class as Profile
//Scaffolding as Create
return View();
}
[HttpPost]
public ActionResult Create(Profile p)//FormCollection collection)
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
p.ID = GetNextProfileID();
profiles.Add(p);
Session["Profiles"]=profiles;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
private int GetNextProfileID()
{
if (Session["Profiles"] != null)
{
profiles = Session["Profiles"] as List<Profile>;
}
return (from p in profiles
select p.ID).Max() + 1;
}
public ViewResult Edit(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
[HttpPost]
public ActionResult Edit(int id, Profile p)
{
try
{
// TODO: Add update logic here
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(pp => pp.ID == id);
profile.Name = p.Name;
profile.Mobile = p.Mobile;
profile.EmailID = p.EmailID;
profile.doj = p.doj;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Details(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
public ActionResult Delete(int id)
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(p => p.ID == id);
return View(profile);
}
[HttpPost]
public ActionResult Delete(int id, Profile p)//FormCollection collection
{
try
{
if(Session["Profiles"] != null)
{
profiles =Session["Profiles"] as List<Profile>;
}
Profile profile = profiles.SingleOrDefault(pp => pp.ID == id);
profiles.Remove(profile);
Session["Profiles"]=profiles;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
No comments:
Post a Comment