• ed by: Parth Shah
  • 0
  • 0
  • May 2020
  • PDF

This document was ed by and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this report form. Report 3i3n4


Overview 26281t

& View Asp.net Core 2.0 Mvc & Razor Pages For Beginners How To Build A Website as PDF for free.

More details 6y5l6z

  • Words: 99,053
  • Pages: 528


Adding a Data Service Hardcoding data in a controller is not good practice. Instead you want to take advantage of dependency injection to make data available in a constructor, using a service component, like the Message service you added earlier. One big benefit of implementing a service is that its interface can be used to implement different components. In this book you will implement one for Mock data and one for a SQL Server database.

54

ASP.NET Core 2.0 MVC & Razor Pages for Beginners In this section, you will implement a MockVideoData component that implements an interface called IVideoData. The data will be implemented as a List

The complete markup for the Details view: @model AspNetVideoCore.ViewModels.VideoViewModel Video 5e691l

68

ASP.NET Core 2.0 MVC & Razor Pages for Beginners
Id: @Model.Id
Title: @Model.Title
Genre: @Model.Genre
@Html.ActionLink("Home", "Index")

The complete markup for the Details action: public IActionResult Details(int id) { var model = _videos.Get(id); if (model == null) return RedirectToAction("Index"); return View(new VideoViewModel { Id = model.Id, Title = model.Title, Genre = Enum.GetName(typeof(Genres), model.GenreId) }); }

Adding a Create View When creating a new record in the data source with a Create view, you have to implement two action methods. The first is a method using HTTP GET to render the Create view in the browser, filling select lists and other controls that need data. The second method is an HTTP POST method that receives data from the client through an HTTP POST request. The post from the client can be done in several ways, for instance with JavaScript or a form post. In this example, you will use a form post to call back to the server when the clicks a Submit button. The HTTP POST action method can fetch data from several places in the posted data: the header, the query string, and the body of the request. The data is then matched against properties in a model object, which is a parameter of the action method. The action can also handle simple types such as int and string, without them being encapsulated in a model object.

69

ASP.NET Core 2.0 MVC & Razor Pages for Beginners There is a naming convention that you need to be aware of, to properly match posted form data with properties in model objects and other parameters. The rule states that the element names in the form data must match the property names to be matched. The default behavior of a view using an enum is to display it as a text field. This is not the best way to display a selected item in a list of values. In this section, you will remove the Video class’s GenreId property, and add a new property of the enum type Genres called Genre. This makes it easier to work with enum data, especially when working with a SQL Server database entity model. You will also add the enum as a property to a new view model called VideoEditViewModel, which can be used both when creating a new video and when editing one.

Refactoring the Application 1. Open the Video class. 2. Delete the GenreId property. 3. Add a using statement to the Models namespace where the Genre enumeration is located. using AspNetVideoCore.Models;

4. Add a new property of type Genres and name it Genre. This property will hold the current genre for the video. public Genres Genre { get; set; }

5. Open the MockVideoData class. 6. Replace the GenreId property with the Genre property and assign its value from the enum directly. new Video { Id = 1, Genre = Models.Genres.Comedy, Title = "Shreck" },

7. Open the HomeController class. 8. Locate the Index action and change the assignment of the Genre string in the VideoViewModel object to use the value stored in the Genre property of the Video object. You can use the ToString method to fetch the name of the enum value. Genre = video.Genre.ToString()

9. Repeat step 7 for the Details action method but use the model variable instead of the video parameter.

70

ASP.NET Core 2.0 MVC & Razor Pages for Beginners 10. Switch to the browser and refresh the application. It should look and work the same as before. The complete code for the Video class, after the changes: public class Video { public int Id { get; set; } public string Title { get; set; } public Genres Genre { get; set; } }

The complete code for the MockVideoData constructor, after the changes: public MockVideoData() { _videos = new List