ASP. NET MVC3.0 Getting Started Guide 5. access model data from Controllers

Source: Internet
Author: User

Access Model data from a controller

  

In this section, you will create a new MoviesController class and write code to read movie data.

Use the view template to display them in the browser. Before proceeding, make sure your application is compiled

Translation.

Right-clickControllersFolder to create a MoviesController. Select the following

Option:

  • Controller name:MoviesController. (Default .)
  • Template:Controller with read/write actions and views, using Entity Framework.
  • Model class:Movie (MvcMovie. Models).
  • Data context class:MovieDBContext (MvcMovie. Models).
  • Views:Razor (CSHTML). (Default .)

Click Add ". Visual Studio creates the following folders and files:

  • Under the Controller folder of the projectMoviesController. csFile
  • In the View folder of the project, the Movies folder
  • In the newly created folderViewsMoviesLowerCreate. cshtml, Delete. cshtml,
    Details. cshtml, Edit. cshtml, AndIndex. cshtml.

  

ASP. net mvc 3 architecture mechanism automatically creates CRUD (create, read, update, and delete)

Response methods and views. You now have all the functions of web applications, supporting adding, displaying,

Edit or delete a movie.

Run the application and append/Movies in the URL bar of the browser to browseMovies controller.

Because the application uses the default route (defined in the Global. asax file) to browse requests

Http: // localhost: xxxxx/MoviesThe default method Index that is routed to the Movies controller.

In other words,Http: // localhost: xxxxx/MoviesActually andHttp: // localhost: xxxxx/Movies/Index

Is the same.The movie list is empty because you have not added anything.

  

Create a movie

  

Select"Create New"Link. Enter the details of a movie and click"Create"Button.

Click"Create"Button to send the page back to the server (where the movie information is

Stored in the database ). Do not redirect/MoviesURL in the list

You can see the newly added movie information.

Create some movies, test all functions, edit, detail, and delete them.

Code Review

OpenControllersMoviesController. csFile and review the generated Index method

Code. The code for some controller Index methods is as follows:

  
public class MoviesController : Controller{    private MovieDBContext db = new MovieDBContext();    //    // GET: /Movies/    public ViewResult Index()    {        return View(db.Movies.ToList());    }}

As described above, the following line instantiates a movie in the MoviesController class

Database content.

private MovieDBContext db = new MovieDBContext();

The Movies controller returns all movie material entities in the database and passes the results to the Index.

View.

Strong model and @ model keywords

  

In the previous section of the tutorial, you learned how to use the ViewBag object to pass data through the Controller

To the view.ViewBag is a dynamic object and provides a convenient late binding method

Information to the view.

ASP. net mvc also supports passing strong data types to view templates. Support this type of strong

Check the code and rich smart awareness during compilation. We willMoviesController class

AndIndex. cshtmlView TemplateThis method is used.

Note that this code calls the View assistant method in the Index method when creating a List object. Code

Pass the movie list to the view through the Controller:

public ViewResult Index(){    return View(db.Movies.ToList());}
 

By adding the @ model expression at the top of the view template file, you can specify

The object type you want to use. When you create a movie controller, Visual Studio

At the top of the view template file Index. cshtml contains the @ model expression:

@model IEnumerable.Models.Movie
 

@ Model command allows you to access a Model object transmitted

View the list of movies. For example,Index. cshtmlTemplate, the Code uses the foreach table

Dashboard traverses movie materials based on strong types.

 
@foreach (var item in Model) {                        @Html.DisplayFor(modelItem => item.Title)                            @Html.DisplayFor(modelItem => item.ReleaseDate)                            @Html.DisplayFor(modelItem => item.Genre)                            @Html.DisplayFor(modelItem => item.Price)                 @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |@Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID })            }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.