Click here to Skip to main content
15,881,559 members
Articles / Web Development / HTML

ASP.NET MVC-5 CRUD Application with drop down list

Rate me:
Please Sign up or sign in to vote.
4.84/5 (24 votes)
27 Aug 2014CPOL2 min read 86.8K   1   37   14
ASP.NET MVC 5 applications

Introduction:

This tutorial will teach you the basics of building an ASP.NET MVC 5 Web application using Visual Studio 2013. I'll show you step-by-step operations using simple screen shorts. So get ready for that journey.

You need to about ASP.NET, SQL Server 2008 R2 & Visual studio 2013.

Image 1

  • Models: Classes that represent the data of the application and that use validation logic to enforce business rules for that data.
  • Views: Template files that your application uses to dynamically generate HTML responses.
  • Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.

How MVC works?

Image 2

Application process:

Image 3

 

Now create the project

  1. Open visual studio 2013, select new project

Image 4 

 

  1. Select ASP.NET Web application & assign name MyMVCApp then just click ok.

 

Image 5

 

  1. Then select MVC & just click ok buttons.

 

Image 6

 

Image 7

 

  1. Now right click into model then add then New Item

Image 8

  1. Now select ADO.NET Entry Data Model and write the name “MyAppEDM” and just click Add button.

 

Image 9

 

  1. Now select Generate from database then click next.

Image 10

 

  1. Now click New connection.

Image 11

 

Image 12

 

Image 13

 

  1. Now type user name and password if you use SQL server authentications & select your database for example dbMYDAT then test connection & just click ok button.

Image 14

 

  1. Now click save entry connection settings and click next.

 

Image 15

 

  1. Now select your desire table, views, Store procedure etc. & then click finished.

 

Image 16

 

Image 17

 

  1. Now create a controller & write it name “StuInfoController”. Before creating controller you have to build the application.

Image 18

Image 19

 

Image 20

Image 21

For this above error you need to rebuild the applications.

 

Image 22

 

  1. Now in RouteConfig just rename the defaults controller "Home" to “StuInfo” controller.

 

Image 23

 

  1. Now just run the application then you probably see the following screen.

 

Image 24

 

  1. Now you can able to create new one.

Image 25

 

Image 26

 

 

Code:

 

Model:

public partial class stuInfo
   {
       [Required]
       public string ID { get; set; }
       [StringLength(20, ErrorMessage = "Max length 30 characters")]
       public string Name { get; set; }
       [Required]
       public string Dept { get; set; }
       [Required]
       public string Subject { get; set; }

       public virtual DeptInfo DeptInfo { get; set; }
       public virtual SubjectInfo SubjectInfo { get; set; }
   }

 

 

Web config:

<connectionStrings>
  <add name="dbMYDATAEntities" connectionString="metadata=res://*/Models.MyAppEDM.csdl|res://*/Models.MyAppEDM.ssdl|res://*/Models.MyAppEDM.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=IT-WS08\SQLEXPRESS;initial catalog=dbMYDATA;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

 

Table script:

Image 27

Stuinfo:

USE [dbMYDATA]

GO


/****** Object:  Table [dbo].[stuInfo]    Script Date: 08/27/2014 12:06:16 ******/

SET ANSI_NULLS ON

GO


SET QUOTED_IDENTIFIER ON

GO


CREATE TABLE [dbo].[stuInfo](

      [ID] [nchar](10) NOT NULL,

      [Name] [nvarchar](max) NULL,

      [Dept] [nchar](10) NULL,

      [Subject] [nchar](10) NULL,

 CONSTRAINT [PK_stuInfo] PRIMARY KEY CLUSTERED

(

      [ID] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO


ALTER TABLE [dbo].[stuInfo]  WITH CHECK ADD  CONSTRAINT [FK_stuInfo_DeptInfo] FOREIGN KEY([Dept])

REFERENCES [dbo].[DeptInfo] ([ID])

GO


ALTER TABLE [dbo].[stuInfo] CHECK CONSTRAINT [FK_stuInfo_DeptInfo]

GO


ALTER TABLE [dbo].[stuInfo]  WITH CHECK ADD  CONSTRAINT [FK_stuInfo_SubjectInfo] FOREIGN KEY([Subject])

REFERENCES [dbo].[SubjectInfo] ([Id])

GO


ALTER TABLE [dbo].[stuInfo] CHECK CONSTRAINT [FK_stuInfo_SubjectInfo]

GO

 

DeptInfo:

USE [dbMYDATA]

GO


/****** Object:  Table [dbo].[DeptInfo]    Script Date: 08/27/2014 12:07:15 ******/

SET ANSI_NULLS ON

GO


SET QUOTED_IDENTIFIER ON

GO


CREATE TABLE [dbo].[DeptInfo](

      [ID] [nchar](10) NOT NULL,

      [Dept] [nvarchar](50) NULL,

 CONSTRAINT [PK_DeptInfo] PRIMARY KEY CLUSTERED

(

      [ID] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]


GO

 

 

SubjectInfo:

USE [dbMYDATA]

GO


/****** Object:  Table [dbo].[SubjectInfo]    Script Date: 08/27/2014 12:07:59 ******/

SET ANSI_NULLS ON

GO


SET QUOTED_IDENTIFIER ON

GO


CREATE TABLE [dbo].[SubjectInfo](

      [Id] [nchar](10) NOT NULL,

      [Subject] [nvarchar](max) NULL,

 CONSTRAINT [PK_SubjectInfo] PRIMARY KEY CLUSTERED

(

      [Id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO

 

Thanks you so much for your patience.

 

References:

http://www.asp.net/mvc

https://www.codeplex.com/

http://www.c-sharpcorner.com/

http://www.codeproject.com/

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

eBook: Programming ASP.NET MVC 5 A Problem Solution Approach

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionNice article...Download seems to be missing. Pin
tiendqhn9-May-19 1:19
tiendqhn9-May-19 1:19 
Questioncode Pin
Member 128152106-Dec-16 5:34
Member 128152106-Dec-16 5:34 
QuestionCreate mvc 5 project with oracle and visual studio 2013 Pin
Veeramani P20-May-15 20:49
Veeramani P20-May-15 20:49 
Questionquestion... Pin
GreatOak14-May-15 7:29
professionalGreatOak14-May-15 7:29 
QuestionVery Good Article Sir Pin
ajaykumar varanasi27-Feb-15 0:52
ajaykumar varanasi27-Feb-15 0:52 
QuestionThe code download is still missing. Pin
pixelmeow19-Jan-15 7:50
professionalpixelmeow19-Jan-15 7:50 
Questiongreat! Pin
Aunebakk29-Oct-14 2:28
Aunebakk29-Oct-14 2:28 
GeneralMy vote of 5 Pin
Christian Amado12-Sep-14 3:37
professionalChristian Amado12-Sep-14 3:37 
GeneralModels?? Pin
Math08avan1-Sep-14 23:48
Math08avan1-Sep-14 23:48 
GeneralRe: Models?? Pin
R M Shahidul Islam Shahed3-Sep-14 0:12
professionalR M Shahidul Islam Shahed3-Sep-14 0:12 
QuestionCode? Pin
treesprite29-Aug-14 11:15
professionaltreesprite29-Aug-14 11:15 
QuestionVote of 5 Pin
Russell_Smith29-Aug-14 1:02
Russell_Smith29-Aug-14 1:02 
GeneralMy vote of 5 Pin
A I Sabuz27-Aug-14 20:04
A I Sabuz27-Aug-14 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.