MVC For Beginners: How to Save Record Using MVC

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:

  1. Open Visual Studio 2010
  2. Go to File > New > Project > (Web) ASP.NET MVC 4 Web Application
  3. In the project templates select Internet Application
  4. View engine (Razor)
  5. 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:

 

  1. USE [comm]  
  2. GO  
  3. /****** Object: Table [dbo].[tblStud] Script Date: 05/01/2015 14:15:33 ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. SET ANSI_PADDING ON  
  9. GO  
  10. CREATE TABLE [dbo].[tblStud](  
  11. [studid] [bigint] NOT NULL,  
  12. [stud_name] [varchar](50) NULL,  
  13. [stud_age] [int] NULL,  
  14. CONSTRAINT [PK_tblStud] PRIMARY KEY CLUSTERED  
  15. (  
  16. [studid] ASC  
  17. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  18. ) ON [PRIMARY]  
  19. GO  
  20. SET ANSI_PADDING OFF  
Note: The table must have a primary key.

View

 

  1. @model projStudentInfo.Models.tblStud  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6. <h2>  
  7. Student Info</h2>  
  8. @using (Html.BeginForm("Save""Studs", FormMethod.Post))  
  9. {  
  10. <table>  
  11.     <tr>  
  12.         <td>  
  13.         Name  
  14.         </td>  
  15.         <td>@Html.TextBoxFor(c => c.stud_name)  
  16.               
  17.         </td>  
  18.     </tr>  
  19.     <tr>  
  20.         <td>  
  21.         Name  
  22.         </td>  
  23.         <td>@Html.TextBoxFor(c => c.stud_age)  
  24.               
  25.         </td>  
  26.     </tr>  
  27.     <tr>  
  28.         <td>  
  29.             <input type="submit" name="submit" value="Save" />  
  30.         </td>  
  31.     </tr>  
  32. </table>  
  33. }  
Controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using projStudentInfo.Models;  
  7. namespace projStudentInfo.Controllers  
  8. {  
  9.     public class StudsController: Controller  
  10.     {  
  11.         //  
  12.         // GET: /Studs/  
  13.         commEntities DB = new commEntities();  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.         public ActionResult Save(tblStud stud)  
  19.         {  
  20.             try  
  21.             {  
  22.                 stud.studid = DB.tblStuds.Select(c = > c.studid).Max() + 1;  
  23.                 DB.tblStuds.AddObject(stud);  
  24.                 DB.SaveChanges();  
  25.                 return RedirectToAction("Index");  
  26.             }   
  27.             catch (Exception ex)  
  28.             {  
  29.             }  
  30.             return RedirectToAction("Index");  
  31.         }  
  32.     }  
  33. }  


Up Next
    Ebook Download
    View all
    Learn
    View all