Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
27 Aug 2014 1  
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.

  • 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?

Application process:

 

Now create the project

  1. Open visual studio 2013, select new project

 

 

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

 

 

  1. Then select MVC & just click ok buttons.

 

 

 

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

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

 

 

  1. Now select Generate from database then click next.

 

  1. Now click New connection.

 

 

 

  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.

 

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

 

 

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

 

 

 

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

 

For this above error you need to rebuild the applications.

 

 

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

 

 

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

 

 

  1. Now you can able to create new one.

 

 

 

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here