Professional Documents
Culture Documents
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Developing MVC Models L3-29
Developing MVC Models
Lab 3: Creating MVC Models
Exercise 1: Exploring the AdventureWorks Database
Task 1: Open the AdventureWorksMvc solution in Visual Studio 2010
1. Log on to the 10264A-GEN-DEV virtual machine as Student, with the
password, Pa$$w0rd.
2. Open Microsoft
Visual Studio
2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
3. Open the AdventureWorksMvc solution at the following location.
Programming Language Location
Visual C#
Visual Studio
2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
3. Open the AdventureWorksMvc solution from the following location.
Programming Language Location
Visual C# D:\Lab Files\CS\Lab 04\Starter\Exercise 01
Visual Basic D:\Lab Files\VB\Lab 04\Starter\Exercise 01
a. In the Start Page Microsoft Visual Studio window, on the File menu,
click Open Project, or press CTRL+SHIFT+O.
b. In the Open Project dialog box, in the File name box, type D:\Lab
Files\CS\Lab 04\Starter\Exercise 01\AdventureWorksMvc.sln or
D:\Lab Files\VB\Lab 04\Starter\Exercise 01\AdventureWorksMvc.sln
and then click Open.
f Task 2: Create a controller named BlogController
1. Create a controller named BlogController in the Controllers folder. The
controller should not include action methods for create, update, delete, and
details scenarios.
a. In Solution Explorer, right-click Controllers, point to Add, and then click
Controller.
b. In the Add Controller dialog box, in the Controller Name box, type
BlogController, and then click Add.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-48 Lab 4: Developing MVC Controllers
2. Add the HandleErrorAttribute attribute to the BlogController class.
[Visual C#]
[HandleError]
[Visual Basic]
<HandleError()>
Annotate the BlogController with the following attribute.
[Visual C#]
[HandleError]
[Visual Basic]
<HandleError()>
3. Verify that the code in the BlogController code file matches the following
code.
[Visual C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AdventureWorksMvc.Controllers
{
[HandleError]
public class BlogController : Controller
{
//
// GET: /Blog/
public ActionResult Index()
{
return View();
}
}
}
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-49
[Visual Basic]
Namespace AdventureWorksMvc
<HandleError()>
Public Class BlogController
Inherits System.Web.Mvc.Controller
'
' GET: /Blog
Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
4. Build the solution, and fix any errors.
In the AdventureWorksMvc Microsoft Visual Studio window, on the
Build menu, click Build Solution, or press CTRL+SHIFT+B.
5. Close Visual Studio 2010.
In the AdventureWorksMvc Microsoft Visual Studio window, click the
Close button.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-50 Lab 4: Developing MVC Controllers
Exercise 2: Adding code to list the existing blog entries
f Task 1: Open the AdventureWorksMvc solution in Visual Studio 2010
1. Open Microsoft Visual Studio 2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
2. Open the AdventureWorksMvc solution from the following location.
Programming Language Location
Visual C# D:\Lab Files\CS\Lab 04\Starter\Exercise 02
Visual Basic D:\Lab Files\VB\Lab 04\Starter\Exercise 02
a. In the Start Page Microsoft Visual Studio window, on the File menu,
click Open Project, or press CTRL+SHIFT+O.
b. In the Open Project dialog box, in the File name box, type D:\Lab
Files\CS\Lab 04\Starter\Exercise 02\AdventureWorksMvc.sln or
D:\Lab Files\VB\Lab 04\Starter\Exercise 02\AdventureWorksMvc.sln
and then click Open.
f Task 2: Add a blogRepository object
1. Import the AdventureWorksMvc.Models namespace in the BlogController
code file.
[Visual C#]
using AdventureWorksMvc.Models;
[Visual Basic]
Not applicable
a. In Solution Explorer, expand Controllers, and then double-click
BlogController.cs or BlogController.vb.
b. In the BlogController code file, insert the following code at the top of the
file.
[Visual C#]
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-51
using AdventureWorksMvc.Models;
[Visual Basic]
Not applicable
2. Declare and instantiate a private BlogRepository object at the top of the
BlogController class.
[Visual C#]
private BlogRepository blogRepository = new BlogRepository();
[Visual Basic]
Private blogRepository As New BlogRepository()
f Task 3: Add code to the Index action method
1. Replace the existing code in the Index action method with code that returns all
of the blog entries to the view, by calling the GetAllBlogs method of the
blogRepository object.
[Visual C#]
var blogs = blogRepository.GetAllBlogs();
return View(blogs);
[Visual Basic]
Dim blogs = blogRepository.GetAllBlogs()
Return View(blogs)
2. Verify that the final Index action method matches the following code:
[Visual C#]
public ActionResult Index()
{
var blogs = blogRepository.GetAllBlogs();
return View(blogs);
}
[Visual Basic]
Function Index() As ActionResult
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-52 Lab 4: Developing MVC Controllers
Dim blogs = blogRepository.GetAllBlogs()
Return View(blogs)
End Function
f Task 4: Create a Blogger action method
1. Add a Blogger action method that takes the ID of the blogger as the only
parameter.
[Visual C#]
public ActionResult Blogger(int id)
{
}
[Visual Basic]
Function Blogger(ByVal id As Integer) As ActionResult
End Function
2. Add code to the Blogger action method to return only the blog entries for the
indicated blogger to the view.
[Visual C#]
var blogs = blogRepository.GetAllBlogs(id);
return View(blogs);
[Visual Basic]
Dim blogs = blogRepository.GetAllBlogs(id)
Return View(blogs)
3. Verify that the final Blogger action method matches the following code.
[Visual C#]
public ActionResult Blogger(int id)
{
var blogs = blogRepository.GetAllBlogs(id);
return View(blogs);
}
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-53
[Visual Basic]
Function Blogger(ByVal id As Integer) As ActionResult
Dim blogs = blogRepository.GetAllBlogs(id)
Return View(blogs)
End Function
f Task 5: Create a Details action method
1. Add a Details action method that takes the ID of the blog entry as its one
parameter.
[Visual C#]
public ActionResult Details(int id)
{
}
[Visual Basic]
Function Details(ByVal id As Integer) As ActionResult
End Function
2. Add code to the Details action method to return only the indicated blog entry,
to the view. However, if the blog entry no longer exists, redirect the user to the
Index view.
[Visual C#]
var blogEntry = blogRepository.GetBlog(id);
if (blogEntry == null)
return RedirectToAction("Index");
return View(blogEntry);
[Visual Basic]
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-54 Lab 4: Developing MVC Controllers
3. Verify that the final Details action method matches the following code.
[Visual C#]
public ActionResult Details(int id)
{
var blogEntry = blogRepository.GetBlog(id);
if (blogEntry == null)
return RedirectToAction("Index");
return View(blogEntry);
}
[Visual Basic]
Function Details(ByVal id As Integer) As ActionResult
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
End Function
4. Build the solution, and fix any errors.
In the AdventureWorksMvc Microsoft Visual Studio window, on the
Build menu, click Build Solution, or press CTRL+SHIFT+B.
5. Close Visual Studio 2010.
In the AdventureWorksMvc Microsoft Visual Studio window, click the
Close button.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-55
Exercise 3: Adding code to create a new blog entry
f Task 1: Open the AdventureWorksMvc solution in Visual Studio 2010
1. Open Microsoft Visual Studio 2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
2. Open the AdventureWorksMvc solution from the following location.
Programming Language Location
Visual C# D:\Lab Files\CS\Lab 04\Starter\Exercise 03
Visual Basic D:\Lab Files\VB\Lab 04\Starter\Exercise 03
a. In the Start Page Microsoft Visual Studio window, on the File menu,
click Open Project, or press CTRL+SHIFT+O.
b. In the Open Project dialog box, in the File name box, type D:\Lab
Files\CS\Lab 04\Starter\Exercise 03\AdventureWorksMvc.sln or
D:\Lab Files\VB\Lab 04\Starter\Exercise 03\AdventureWorksMvc.sln
and then click Open.
f Task 2: Create a Create action method
1. Add a Create action method in the BlogController class.
[Visual C#]
public ActionResult Create()
{
}
[Visual Basic]
Function Create() As ActionResult
End Function
a. In Solution Explorer, expand Controllers, and then double-click
BlogController.cs or BlogController.vb.
b. In the BlogController code file, add the following method to the
BlogController class.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-56 Lab 4: Developing MVC Controllers
[Visual C#]
public ActionResult Create()
{
}
[Visual Basic]
Function Create() As ActionResult
End Function
2. Add code to the Create action method to return an empty ActionResult, to the
view.
[Visual C#]
return View();
[Visual Basic]
Return View()
3. Verify that the final Create action method matches the following code:
[Visual C#]
public ActionResult Create()
{
return View();
}
[Visual Basic]
Function Create() As ActionResult
Return View()
End Function
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-57
f Task 3: Create a Create action method for an HTTP Post request
1. Add a Create action method for an HTTP Post request that takes a Blog object
as the only parameter.
[Visual C#]
[HttpPost]
public ActionResult Create(Blog blog)
{
}
[Visual Basic]
<HttpPost()>
Function Create(ByVal blg As Blog) As ActionResult
End Function
2. Add code to the Create action method to save the new Blog object and redirect
the user to the Index view. However, if the call to the Save method fails, return
the user to the Create view.
[Visual C#]
if (!ModelState.IsValid)
return View(blog);
try
{
blog.DatePosted = DateTime.Now;
blogRepository.AddBlog(blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
added.";
return RedirectToAction("Index");
}
catch (Exception)
{
return View(blog);
};
[Visual Basic]
If ModelState.IsValid Then
Try
blg.DatePosted = DateTime.Now
blogRepository.AddBlog(blg)
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-58 Lab 4: Developing MVC Controllers
blogRepository.Save()
TempData("ConfirmationMessage") = blg.Title & " has been
added"
Return RedirectToAction("Index")
Catch
Return View(blg)
End Try
End If
Return View(blg)
3. Verify that the final Create action method that for the HTTP Post request
matches the following code.
[Visual C#]
[HttpPost]
public ActionResult Create(Blog blog)
{
if (!ModelState.IsValid)
return View(blog);
try
{
blog.DatePosted = DateTime.Now;
blogRepository.AddBlog(blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
added.";
return RedirectToAction("Index");
}
catch (Exception)
{
return View(blog);
};
}
[Visual Basic]
<HttpPost()>
Function Create(ByVal blg As Blog) As ActionResult
If ModelState.IsValid Then
Try
blg.DatePosted = DateTime.Now
blogRepository.AddBlog(blg)
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-59
blogRepository.Save()
TempData("ConfirmationMessage") = blg.Title & " has
been added"
Return RedirectToAction("Index")
Catch
Return View(blg)
End Try
End If
Return View(blg)
End Function
4. Build the solution, and fix any errors.
In the AdventureWorksMvc Microsoft Visual Studio window, on the
Build menu, click Build Solution, or press CTRL+SHIFT+B.
5. Close Visual Studio 2010.
In the AdventureWorksMvc Microsoft Visual Studio window, click the
Close button.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-60 Lab 4: Developing MVC Controllers
Exercise 4: Adding code to edit a blog entry
f Task 1: Open the AdventureWorksMvc solution in Visual Studio 2010
1. Open Microsoft Visual Studio 2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
2. Open the AdventureWorksMvc solution from the following location.
Programming Language Location
Visual C# D:\Lab Files\CS\Lab 04\Starter\Exercise 04
Visual Basic D:\Lab Files\VB\Lab 04\Starter\Exercise 04
a. In the Start Page Microsoft Visual Studio window, on the File menu,
click Open Project, or press CTRL+SHIFT+O.
b. In the Open Project dialog box, in the File name box, type D:\Lab
Files\CS\Lab 04\Starter\Exercise 04\AdventureWorksMvc.sln or
D:\Lab Files\VB\Lab 04\Starter\Exercise 04\AdventureWorksMvc.sln
and then click Open.
f Task 2: Create an Edit action method
1. Add an Edit action method that takes the ID of a blog entry as the only
parameter. The method should be added to the BlogController class.
[Visual C#]
public ActionResult Edit(int id)
{
}
[Visual Basic]
Function Edit(ByVal id As Integer) As ActionResult
End Function
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-61
a. In Solution Explorer, expand Controllers, and then double-click
BlogController.cs or BlogController.vb.
b. In the BlogController code file, add the following method to the
BlogController class.
[Visual C#]
public ActionResult Edit(int id)
{
}
[Visual Basic]
Function Edit(ByVal id As Integer) As ActionResult
End Function
2. Add code to the Edit action method to return the indicated Blog entry, to the
view. However, if the blog entry no longer exists, redirect the user to the Index
view.
[Visual C#]
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
return View(blog);
[Visual Basic]
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
3. Verify that the final Edit action method matches the following code.
[Visual C#]
public ActionResult Edit(int id)
{
var blog = blogRepository.GetBlog(id);
if (blog == null)
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-62 Lab 4: Developing MVC Controllers
return RedirectToAction("Index");
return View(blog);
}
[Visual Basic]
Function Edit(ByVal id As Integer) As ActionResult
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
End Function
f Task 3: Create an Edit action method for an HTTP Post request
1. Add an Edit action method for an HTTP Post request that takes the ID of the
blog entry and the FormCollection as the two parameters.
[Visual C#]
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
}
[Visual Basic]
<HttpPost()>
Function Edit(ByVal id As Integer, ByVal collection As
FormCollection) As ActionResult
End Function
2. Add code to the Edit action method to save the blog entry and redirect the
user to the Index view. However, if the call to the Save method fails, return the
user to the Edit view. Also, if the blog entry no longer exists, redirect the user
to the Index view.
[Visual C#]
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
try
{
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-63
UpdateModel(blog, collection.ToValueProvider());
blogRepository.UpdateBlog(blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
updated.";
return RedirectToAction("Index");
}
catch (Exception)
{
return View(blog);
};
[Visual Basic]
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Try
UpdateModel(blogEntry, collection.ToValueProvider())
blogRepository.UpdateBlog(CType(blogEntry, Blog))
blogRepository.Save()
TempData("ConfirmationMessage") = blogEntry.Title & " has been
updated"
Return RedirectToAction("Index")
Catch
Return View(blogEntry)
End Try
3. Verify that the final Edit action method for an HTTP Post request matches the
following code.
[Visual C#]
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
try
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-64 Lab 4: Developing MVC Controllers
{
UpdateModel(blog, collection.ToValueProvider());
blogRepository.UpdateBlog(blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
updated.";
return RedirectToAction("Index");
}
catch (Exception)
{
return View(blog);
};
}
[Visual Basic]
<HttpPost()>
Function Edit(ByVal id As Integer, ByVal collection As
FormCollection) As ActionResult
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Try
UpdateModel(blogEntry, collection.ToValueProvider())
blogRepository.UpdateBlog(CType(blogEntry, Blog))
blogRepository.Save()
TempData("ConfirmationMessage") = blogEntry.Title & " has
been updated"
Return RedirectToAction("Index")
Catch
Return View(blogEntry)
End Try
End Function
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-65
4. Build the solution, and fix any errors.
In the AdventureWorksMvc Microsoft Visual Studio window, on the
Build menu, click Build Solution, or press CTRL+SHIFT+B.
5. Close Visual Studio 2010.
In the AdventureWorksMvc Microsoft Visual Studio window, click the
Close button.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-66 Lab 4: Developing MVC Controllers
Exercise 5: Adding code to delete a blog entry
f Task 1: Open the AdventureWorksMvc solution in Visual Studio 2010
1. Open Microsoft Visual Studio 2010.
On the Start menu of 10264A-GEN-DEV, point to All Programs, click
Microsoft Visual Studio 2010, and then click Microsoft Visual Studio
2010.
2. Open the AdventureWorksMvc solution from the following location.
Programming Language Location
Visual C# D:\Lab Files\CS\Lab 05\Starter\Exercise 03
Visual Basic D:\Lab Files\VB\Lab 05\Starter\Exercise 03
In the Start Page Microsoft Visual Studio window, on the File menu, click
Open Project, or press CTRL+SHIFT+O.
In the Open Project dialog box, in the File name box, type D:\Lab
Files\CS\Lab 04\Starter\Exercise 05\AdventureWorksMvc.sln or
D:\Lab Files\VB\Lab 04\Starter\Exercise 05\AdventureWorksMvc.sln
and then click Open.
f Task 2: Create a Delete action method
1. Add a Delete action method that takes the ID of a blog entry as the only
parameter. The method should be added to the BlogController class.
[Visual C#]
public ActionResult Delete(int id)
{
}
[Visual Basic]
Function Delete(ByVal id As Integer) As ActionResult
End Function
a. In Solution Explorer, expand Controllers, and then double-click
BlogController.cs or BlogController.vb.
b. In the BlogController code file, add the following method to the
BlogController class.
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-67
[Visual C#]
public ActionResult Delete(int id)
{
}
[Visual Basic]
Function Delete(ByVal id As Integer) As ActionResult
End Function
2. Add code to the Delete action method to redirect to the Index view.
[Visual C#]
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
return View(blog);
[Visual Basic]
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
3. Verify that the final Delete action method matches the following code.
[Visual C#]
public ActionResult Delete(int id)
{
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
return View(blog);
}
[Visual Basic]
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-68 Lab 4: Developing MVC Controllers
Function Delete(ByVal id As Integer) As ActionResult
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
Return View(blogEntry)
End Function
f Task 3: Create a Delete action method for an HTTP Post request
1. Add a Delete action method for an HTTP Post request that takes the ID of the
blog entry and a confirmation string as the two parameters.
[Visual C#]
[HttpPost]
public ActionResult Delete(int id, string confirmation)
{
}
[Visual Basic]
<HttpPost()>
Function Delete(ByVal id As Integer, ByVal confirmation As String)
As ActionResult
End Function
2. Add code to the Delete action method to delete the blog entry and redirect the
user to the Index view.
[Visual C#]
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
blogRepository.DeleteBlog((Blog) blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
deleted.";
return RedirectToAction("Index");
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
Module 4: Developing MVC Controllers L4-69
[Visual Basic]
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
blogRepository.DeleteBlog(CType(blogEntry, Blog))
blogRepository.Save()
TempData("ConfirmationMessage") = blogEntry.Title & " has been
deleted."
Return RedirectToAction("Index")
3. Verify that the final Delete action method for an HTTP Post request matches
the following code.
[Visual C#]
[HttpPost]
public ActionResult Delete(int id, string confirmation)
{
var blog = blogRepository.GetBlog(id);
if (blog == null)
return RedirectToAction("Index");
blogRepository.DeleteBlog((Blog) blog);
blogRepository.Save();
TempData["ConfirmationMessage"] = blog.Title + " has been
deleted.";
return RedirectToAction("Index");
}
[Visual Basic]
<HttpPost()>
Function Delete(ByVal id As Integer, ByVal confirmation As String)
As ActionResult
Dim blogEntry = blogRepository.GetBlog(id)
If blogEntry Is Nothing Then
Return RedirectToAction("Index")
End If
blogRepository.DeleteBlog(CType(blogEntry, Blog))
M
C
T
U
S
E
O
N
L
Y
.
S
T
U
D
E
N
T
U
S
E
P
R
O
H
I
B
I
T
E
D
L4-70 Lab 4: Developing MVC Controllers
blogRepository.Save()
TempData("ConfirmationMessage") = blogEntry.Title & " has been
deleted."
Return RedirectToAction("Index")
End Function
4. Build the solution, and fix any errors.
In the AdventureWorksMvc Microsoft Visual Studio window, on the
Build menu, click Build Solution, or press CTRL+SHIFT+B.
5. Close Visual Studio 2010.
In the AdventureWorksMvc Microsoft Visual Studio window, click the
Close button.
f Task 4: Turn off the virtual machine and revert the changes
1. In Microsoft Hyper-V Manager, in the Virtual Machines pane, right-click
10264A-GEN-DEV, and then click Turn Off.
2. In the Turn Off Machine dialog box, click Turn Off.
3. In Hyper-V Manager, in the Virtual Machines pane, right-click 10264A-GEN-
DEV, and then click Revert.
4. In the Revert Virtual Machine dialog box, click Revert.