This article provides a very simple procedure for creating a MVC application, however it is something more than a “Hello World”. This article will help you to understand how MVC works. I will create a simple insert operation of Student Info.
We will go step-by-step from the beginning:
- Open Visual Studio 2010
- Go to File > New > Project > (Web) ASP.NET MVC 4 Web Application
- In the project templates select Internet Application
- View engine (Razor)
- OK
After creating the project you will see the following in the Solution Explorer.
Now let's first add a controller for StudentInfo.
Procedure to add a new Controller
Right-click on the Controller folder.
Type controller name
Here I have used the name Studs and the word Controller should be there. I have selected the empty controller template.
You will see default code with one ActionResult with the name Index. Now run the project and go to the URL http://localhost:(generatedport)/Studs/Index . You just need to add /Studs/Index at the end and you will get the following result:
This is because you have created an action but not a HTML page for this action (index). Now I will add a view page for this ActionResult Index.
To create a view, right-click on Index() in the Controller code then add a View with the name Index. The View name must be the same as the ActionName.
See the following Solution Explorer to see the automatic view folder of Studs that was created.
Now we will add a Model. Right-click on Model then select New Item > Data template (ADO.NET Entity Data Model).
Name your model “MyModel” then select Generate From Database then enter your configuration then select your table.
I have created tblStud as in the following:
- USE [comm]
- GO
-
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[tblStud](
- [studid] [bigint] NOT NULL,
- [stud_name] [varchar](50) NULL,
- [stud_age] [int] NULL,
- CONSTRAINT [PK_tblStud] PRIMARY KEY CLUSTERED
- (
- [studid] 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
- SET ANSI_PADDING OFF
Note: The table must have a primary key.
View
- @model projStudentInfo.Models.tblStud
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>
- Student Info</h2>
- @using (Html.BeginForm("Save", "Studs", FormMethod.Post))
- {
- <table>
- <tr>
- <td>
- Name
- </td>
- <td>@Html.TextBoxFor(c => c.stud_name)
-
- </td>
- </tr>
- <tr>
- <td>
- Name
- </td>
- <td>@Html.TextBoxFor(c => c.stud_age)
-
- </td>
- </tr>
- <tr>
- <td>
- <input type="submit" name="submit" value="Save" />
- </td>
- </tr>
- </table>
- }
Controller
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using projStudentInfo.Models;
- namespace projStudentInfo.Controllers
- {
- public class StudsController: Controller
- {
-
-
- commEntities DB = new commEntities();
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult Save(tblStud stud)
- {
- try
- {
- stud.studid = DB.tblStuds.Select(c = > c.studid).Max() + 1;
- DB.tblStuds.AddObject(stud);
- DB.SaveChanges();
- return RedirectToAction("Index");
- }
- catch (Exception ex)
- {
- }
- return RedirectToAction("Index");
- }
- }
- }