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
- Open visual studio 2013, select new project
- Select ASP.NET Web application & assign name MyMVCApp then just click ok.
- Then select MVC & just click ok buttons.
- Now right click into model then add then New Item
- Now select ADO.NET Entry Data Model and write the name “MyAppEDM” and just click Add button.
- Now select Generate from database then click next.
- Now click New connection.
- 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.
- Now click save entry connection settings and click next.
- Now select your desire table, views, Store procedure etc. & then click finished.
- 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.
- Now in RouteConfig just rename the defaults controller "Home" to “StuInfo” controller.
- Now just run the application then you probably see the following screen.
- 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="data source=IT-WS08\SQLEXPRESS;initial catalog=dbMYDATA;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Table script:
Stuinfo:
USE [dbMYDATA]
GO
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
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
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