Pages

Friday, January 29, 2016

Data to MVC in 10 minutes.



Database

Assuming you created your table in SSMS already, It should look something like this:
Now drag select the 3 columns like and control+C copy them to the clipboard:
…and the clipboard…
ID              uniqueidentifier     Unchecked
International   bit                  Unchecked
Name            nvarchar(20)         Unchecked
Description     nvarchar(200)        Checked
BarPercent      int                  Unchecked
IncludesSteps   varchar(MAX)         Unchecked

Model

Go to Visual Studio and in your Solution Explorer, right click Models and Add, then Class
Make it easy on yourself:  Name it the same as your table.
Make the code look like this…
namespace SA.DS._0._2.Models  //this should match your other models, or leave it how Visual Studio created it
{
        using System;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Data.Entity.Spatial;

        [Table("Lookups.StatusBarTypes")]  //This is the name of your table.  If the schema is left out, it assumes [dbo]
        public partial class StatusBarTypes
        {
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]

                //a default constructor
                public StatusBarTypes()
                {

                }

        }
}
Now, we'll paste in the list from the clipboard.
                //a default constructor
                public StatusBarTypes()
                {

                }


                ID      uniqueidentifier        Unchecked
International   bit     Unchecked
Name    nvarchar(20)    Unchecked
Description     nvarchar(200)   Checked
BarPercent      int     Unchecked
IncludesSteps   varchar(MAX)    Unchecked
                Unchecked
        }
}
Then doctor it into object properties.  DO NOT rename any fields!  Make sure anything that says "Checked" is nullable.
Keep tabs on the string lengths and whether the columns are required or not. (not null means Requred)
                [Key]
                public Guid ID { get; set; }

                [Required]
                public bool International { get; set; }

                [Required]
                [StringLength(20)]
                public string Name { get; set; }

                [StringLength(200)]
                public string? Description { get; set; }

                [Required]
                public int BarPercent { get; set; }

                public int IncludesSteps { get; set; }

If you want, you can add display info, like prettier column names.
                [Key]
                public Guid ID { get; set; }

                [Required]
                [Display(Name="Intl")]
                public bool International { get; set; }

                [Required]             
                [StringLength(20)]
                public string Name { get; set; }

                [StringLength(200)]
                public string Description { get; set; }

                [Required]
                [Display(Name="Bar%")]
                public int BarPercent { get; set; }

                [Display(Name="Incl Steps")]
                public int IncludesSteps { get; set; }

OK save it and BUILD.
If all went well, we can…

Controller and View

In Controllers, right click Add, then Controller
Pick MVC5 Controller with Views, Using Entity Framework
Click Add.
Pick your new Model Class.
Make sure it's your correct Database, pick a layout page, and Add.
Note that it autogenerates your controller…
…and your CRUD views.
These Views are ALL WIRED UP and ready to use.  Open Index.cshtml in Visual Studio and run it.
Your empty data table awaits!  Add some records, edit them, delete them, modify them!





Share This!

Contact Us

Name

Email *

Message *