"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

Controller methods and views

By Rick Anderson

We have a good start to the movie app, but the presentation is not ideal. We don’t want to see the time (12:00:00 AM in the image below) and ReleaseDate should be two words.

Index view: Release Date is one word (no space) and every movie release date shows a time of 12 AM
Index view: Release Date is one word (no space) and every movie release date shows a time of 12 AM

Open the Models/Movie.cs file and add the highlighted lines shown below:

[!code-csharpMain]

   1:  #if AddDate
   2:  #region snippet_1
   3:  using System;
   4:  using System.Collections.Generic;
   5:  using System.Linq;
   6:  using System.Threading.Tasks;
   7:   
   8:  namespace MvcMovie.Models
   9:  {
  10:      public class Movie
  11:      {
  12:          public int ID { get; set; }
  13:          public string Title { get; set; }
  14:   
  15:          [Display(Name = "Release Date")]
  16:          [DataType(DataType.Date)]
  17:          public DateTime ReleaseDate { get; set; }
  18:          public string Genre { get; set; }
  19:          public decimal Price { get; set; }
  20:      }
  21:  }
  22:  #endregion
  23:  #endif

Right click on a red squiggly line > Quick Actions and Refactorings.

Contextual menu shows > Quick Actions and Refactorings.
Contextual menu shows > Quick Actions and Refactorings.

Tap using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations at top of list
using System.ComponentModel.DataAnnotations at top of list

Visual studio adds using System.ComponentModel.DataAnnotations;.

Let’s remove the using statements that are not needed. They show up by default in a light grey font. Right click anywhere in the Movie.cs file > Remove and Sort Usings.

Remove and Sort Usings
Remove and Sort Usings

The updated code:

[!code-csharpMain]

   1:  //#define AddDate 
   2:  #if AddDate
   3:  #region snippet_1
   4:  using System;
   5:  using System.ComponentModel.DataAnnotations;
   6:   
   7:  namespace MvcMovie.Models
   8:  {
   9:      public class Movie
  10:      {
  11:          public int ID { get; set; }
  12:          public string Title { get; set; }
  13:   
  14:          [Display(Name = "Release Date")]
  15:          [DataType(DataType.Date)]
  16:          public DateTime ReleaseDate { get; set; }
  17:          public string Genre { get; set; }
  18:          public decimal Price { get; set; }
  19:      }
  20:  }
  21:  #endregion
  22:  #endif

[!INCLUDEadding-model]

Previous Next





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnetcore/tutorials/first-mvc-app/controller-methods-views.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>