1. Create a new project with MVC2.
2. It asks whether to create a unit test project. Check “no, do not create a test project
3. The structure of the solution explorer is as follow
4. The project is divided into 3 parts – controller, Model, View.
5. Right click Models -> add -> New Item.
6.Create a table as "EMP" in the database.
7. Right click on model folder
8.Select a LINQ to SQL Classes
9. then click on Add.
10.Drag the table from the database to ORM area.
11. Now add a controller to do any actions on the model.
a. Right click on controller -> Add -> Controller
b. Give the controller Name – ‘Home’. Note that the ‘controller’ suffix is added by default.
c. Check – Add action methods for Create, Update, Delete, Details. And give ‘Add’.
d.
This adds a new controller called ’HomeController’ under Controller
folder with action methods for Index, Create, Edit, Details, Delete.
12. Now, build the application. And check for no errors.
13.Then right click on the any method name from the ’HomeController’ page select Add View.
14.Create view for the ‘Index’ method to display the EMP table values.
a. Right click on the Index method and give ’Add View’.
b. Give the View Name, and check – Create a strongly typed view.
c. In the view data-class dropdown, select the namespace
" MvcApplication22.Models.EMP"
d. In View content drop down, select ‘List’.
e. Give ‘Add’.
15.Go to the HomeController page add a name space on the top
"using MvcApplication22.Models"
Add a object of the DataClasses1DataContext calass to the top
CODE OF THE HOME CONTROLLER PAGE :
" DataClasses1DataContext o = new DataClasses1DataContext(); "
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication22.Models;
namespace MvcApplication22.Controllers
{
public class HomeController : Controller
{
DataClasses1DataContext o = new DataClasses1DataContext();
public ActionResult Index()
{
return View();
}
public ActionResult DISPLAY()
{
return View(o.EMPs.ToList());
}
public ActionResult Details(int id)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(EMP p)
{
try
{
o.EMPs.InsertOnSubmit(p);
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
[HttpPost]
public ActionResult Edit(int id,EMP p)
{
try
{
var m = (from x in o.EMPs where x.EID == id select x).First();
m.NAME = p.NAME;
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Delete(int id,string s)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
[HttpPost]
public ActionResult Delete(int id)
{
try
{
var m = (from x in o.EMPs where x.EID == id select x).First();
o.EMPs.DeleteOnSubmit(m);
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
PAGE LOOKS LIKE.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication22.Models;
namespace MvcApplication22.Controllers
{
public class HomeController : Controller
{
DataClasses1DataContext o = new DataClasses1DataContext();
public ActionResult Index()
{
return View();
}
public ActionResult DISPLAY()
{
return View(o.EMPs.ToList());
}
public ActionResult Details(int id)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(EMP p)
{
try
{
o.EMPs.InsertOnSubmit(p);
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
[HttpPost]
public ActionResult Edit(int id,EMP p)
{
try
{
var m = (from x in o.EMPs where x.EID == id select x).First();
m.NAME = p.NAME;
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Delete(int id,string s)
{
var m = (from x in o.EMPs where x.EID == id select x).First();
return View(m);
}
[HttpPost]
public ActionResult Delete(int id)
{
try
{
var m = (from x in o.EMPs where x.EID == id select x).First();
o.EMPs.DeleteOnSubmit(m);
o.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
PAGE LOOKS LIKE.
No comments:
Post a Comment