Tuesday, November 9, 2010

Basic ASP.Net MVC Site



In this post I will demonstrate how easy it is to create an ASP MVC site with basic CRUD operations on the Chinook database.

  1. Start Visual Studio and create a new project using the ASP.Net MVC 2 template. I'm a proponent of Unit Testing, but for today's demo let's do not create the unit tests.
  2. Right-click the Models folder and select Add->New Item->Data->ADO.Net Entity Data Model.
  3. Name the Model Chinook.edmx and choose the Artists table to import into the model.
  4. Right-click the Controllers folder and select Add->Controller. Name the controller ArtistController and check the box to Add action methods for our CRUD operations.
  5. Next, In the Index function select all artists from the Artists table in the Chinook database like this...
public ActionResult Index()
{
using (ChinookEntities chinook = new ChinookEntities())
{
var allArtists = from artists in chinook.Artists
orderby artists.Name
select artists;

return View(allArtists.ToList());
}
}
  • Right-click "View(allArtists)" and select Add View->Check the box for Create Strongly-Typed View->Select ChinookArist.Models.Artist->Select List for View Content->Hit the Add button.
  • Now add a link to our controller in the Site.Master and we're ready to go.
  • Run the project and hit the link to Artists and you get a list of all artists in the table.





To add the details view to our project add the following snippet to the Details(int id) function in ArtistController.cs...

public ActionResult Details(int id)
{
using (ChinookEntities chinook = new ChinookEntities())
{
var artist = (from a in chinook.Artists
where a.ArtistId == id
select a).First();

return View(artist);
}
}
Next, right-click "Details" and select Add View. Be sure to select Details for our View content drop-down. Voila, a Details.aspx page is create for us.

Editing a record works in a similar manner. Add the following code to ArtistController...
public ActionResult Edit(int id)
{
using (ChinookEntities chinook = new ChinookEntities())
{
var artist = (from a in chinook.Artists
where a.ArtistId == id
select a).First();

return View(artist);
}
}

This will return the record to edit. Right-click the Edit and follow the steps to create and edit page. In addition, you will need the following which will actually perform the editing...
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
using (ChinookEntities chinook = new ChinookEntities())
{
var artist = (from a in chinook.Artists
where a.ArtistId == id
select a).First();

artist.Name = collection[1].ToString();
chinook.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}



To creating a new record use the following...
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
using (ChinookEntities chinook = new ChinookEntities())
{
Artist artist = new Artist();
artist.Name = collection[0].ToString();
chinook.AddToArtists(artist);
chinook.SaveChanges();
}

return RedirectToAction("Index");
}
catch
{
return View();
}
}

Right-click Create and a new aspx page is created for us.

Deleting a record is similar to editing in that it requires two functions...
public ActionResult Delete(int id)
{
using (ChinookEntities chinook = new ChinookEntities())
{
var artist = (from a in chinook.Artists
where a.ArtistId == id
select a).First();

return View(artist);
}
}

and...

[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
using (ChinookEntities chinook = new ChinookEntities())
{
var artist = (from a in chinook.Artists
where a.ArtistId == id
select a).First();
chinook.DeleteObject(artist);
chinook.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}

Once again, right-clicking the Delete function and following the prompts will create a delete.aspx page for us.


In just a few minutes I have created a simple, yet fully functional MVC application with complete CRUD operations on a table.


No comments:

Post a Comment