Implementing Edit, Details, and Delete Views
Open the Movie
controller and add the following Details
method:
public ActionResult Details(int id)
{
Movie movie = db.Movies.Find(id);
if(movie == null)
return RedirectToAction("Index");
return View("Details", movie);
}
The code-first approach makes it easy to search for data using the Find
method. An important security feature of this method is that we actually verify that we found a movie. For example, a hacker could introduce errors into the site by changing the URL created by the links from http://localhost:xxxx/Movies/Details/1 to http://localhost:xxxx/Movies/Details/12345. Without the check for a null movie, this could result in a database error.
Right-click inside the Details
method and select Add View. For Scaffold template, choose Details.
Run the application and select a Details link.
Implementing an Edit View
Back in the Movie
controller, add the following Edit
methods:
public ActionResult Edit(int id)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
return RedirectToAction("Index");
return View(movie);
}
[HttpPost]
public ActionResult Edit(Movie model)
{
try {
var movie = db.Movies.Find(model.ID);
UpdateModel(movie);
db.SaveChanges();
return RedirectToAction("Details", new { id = model.ID });
} catch (Exception)
{
ModelState.AddModelError("", "Edit Failure, see inner exception");
}
return View(model);
}
The first Edit
method will be called when a user clicks one of the edit links. If the movie is found, the application will display the movie data in the Edit view. The Edit
method marked with [HttpPost]
takes a movie object created by the model binder from data posted in the Edit form. The model copier copies the edited data into the movie entry in the database. If any errors occur while the data is being saved to the database, the user is redirected to the Edit view with the data that was posted. Right-click inside the Edit
method and select Add View. For Scaffold template, choose Edit.
Run the application, select an Edit link, and try editing some data.
Implementing a Delete View
Add the following Delete
methods to the Movie
controller.
public ActionResult Delete(int id)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
return RedirectToAction("Index");
return View(movie);
}
[HttpPost]
public RedirectToRouteResult Delete(int id, FormCollection collection)
{
Movie movie = db.Movies.Find(id);
db.Movies.Remove(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
Note that the Delete
method that isn't marked with [HttpPost]
does not delete the data. Performing a delete operation in response to a GET request (or for that matter, performing an edit operation, create operation, or any other operation that changes data) opens up a security hole. For more information on this, see Stephen Walther's blog entry ASP.NET MVC Tip #46 — Don't use Delete Links because they create Security Holes.
Right-click inside the Delete
method and select Add View. Select the Delete scaffold template.
We now have a complete MVC application that stores data in a SQL Server Express database. We can create, read, update, and delete movies.
This basic tutorial got you started making controllers, associating them with views, and passing around hard-coded data. Then we created and designed a data model. The code-first approach created a database from the data model on the fly. We retrieved the data from the database and displayed it in an HTML table. Then we added a Create form that let users add data to the database. We added validation by marking the data model with attributes from the DataAnnotations
namespace. The resulting validation runs on the client and on the server. We changed the database to include a new column of data, then updated two pages to create and display this new data. Finally, we added code and view templates to support Edit, Details, and Delete actions.
I now encourage you to move on to our intermediate-level MVC Music Store tutorial and to check out the many videos and resources at http://asp.net/mvc to learn even more about ASP.NET MVC!
Enjoy!
分享到:
相关推荐
Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes ...
Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes ...
A Visual Studio 2013 project which shows how to use the Entity Framework 6 in an ASP.NET MVC 5 web application project, using the Code First development approach. The previous version that uses EF 5 ...
With this practical book, you’ll learn how by combining the ASP.NET MVC server-side language, the Bootstrap front-end framework, and Knockout.js—the JavaScript implementation of the Model-View-...
Wrox.ASP.NET.2.0.Website.Programming.Problem.Design.Solution.May.2006 CHM格式 Overview Dear reader, thanks for picking up this book, and welcome to the new edition of ASP.NET Website ...
Web API Client Implementations – ASP.NET MVC and jQuery Scaffolding with Web API – Entity Framework Routing in Web API Implementing Multiple Serialization Options Help Page Generation Table of ...
This book provides a complete guide to implementing Telerik’s range of ASP.NET and Silverlight controls. Telerik controls are invaluable for ASP.NET and Silverlight developers because they provide a ...
Mastering the nuts and bolts of ASP.NET IIS and Web applications Using the ASP.NET event model to control the entire user request lifecycle Creating special handlers for special requests Building ...
在ASP.NET MVC框架中,为你的Web应用实施两因素身份验证(2FA)是一种增强用户安全性的有效方法。Google Authenticator是一款广泛使用的2FA工具,它通过生成一次性密码(OTP)来提供额外的安全层。本篇文章将深入...
在ASP.NET MVC框架中,确保模型属性的唯一性是数据完整性和一致性的重要组成部分。这通常涉及到数据库中的唯一约束,但也可以通过应用层逻辑来强化。本系列文章的第二部分将深入探讨实现这一目标的高效策略。 首先...
1 ■ Introducing ASP.NET AJAX 3 2 ■ First steps with the Microsoft Ajax Library 36 3 ■ JavaScript for Ajax developers 73 4 ■ Exploring the Ajax server extensions 114 5 ■ Making asynchronous ...
Arduino Programming using .NET and Sketch shows readers how to do so with practical Arduino projects, such as preparing a development environment, performing sensing and actuating with external ...
You’ll learn how to gain more value from existing Microsoft technologies such as ASP.NET MVC and SignalR by using them alongside other technologies such as Bootstrap, AJAX, JSON, and JQuery....
architecture on the server side using ASP.NET Core MVC. You will learn about the startup configuration, authentication, routes, models, views, and controllers that make up ASP.NET Core MVC. Chapter
C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development – Third Edition 版本: Create powerful applications with .NET Standard 2.0, ASP.NET Core 2.0, … Visual Studio 2017 or Visual Studio Code ...
Work at your own pace through the lessons and hands-on exercises to learn how to build Web applications using Visual Basic .NET and Visual C# .NET. Then extend your expertise through additional skill...
在ASP.NET 2.0中,保护连接字符串和其他设置信息是确保应用程序安全性的重要环节。连接字符串通常包含了数据库的用户名、密码、服务器名称等敏感信息,这些数据如果不加以保护,可能会被恶意用户利用,导致数据泄露...
Reengineer .NET Code to Improve Quality, Update Architecture, Access New Tools, and Accelerate Delivery of New Features As software ages, it becomes brittle: difficult to understand, fix, manage, use...