Adding a record into data base using MVC

Add a new record into a data base table using MVC

Emp display .aspx

EMP IDENameAddressSalary
101
102
103

Technologies

 

Add name Employee

                 3

 Link button1

Enter EmpID
Enter Ename
Enter Design
Enter DOJ
Enter salary
Enter Dept
  Save

Technologies

 

Back to EmpDetails

         3

Link Button

Step1:

Creating the data model

       ↓

Crate a new MVC application with the name

(Example 4)

       ↓

Go to solution explorer

       ↓

Select the folder model

       ↓3

Click with RM button

       ↓

Click on add

       ↓

Click on new item

       ↓

Select data from visual C# in installed template

       ↓

Select ADO.net entity data model template

       ↓

Type the name (Employee.EDMX)

       ↓

Click on add

2Select generate from D/B  2 click on next

       ↓

Click on new connection button

       ↓

Type the server name as (.:)

       ↓

Select radio button use SQL server authentication

       ↓

Type username, password (“sa”,”abc”)

       ↓

Activate the checkbox save my password

       ↓

Select the D/B employee

       ↓

Click on ok

       ↓

Activate radio button (yes include sensitive data)

       ↓

Click on next

       ↓

This will retrieve all data base table

       ↓

Select Emp details table]

       ↓

Click on finish

 

Step2: 

Creating action method in home controller for Emp display

Go to home controller.cs

       ↓

Write the following code

Using example. Models  -->  name space

Public action result Emp display ()

{

Employee entities.obj=new employee entities ();

Return view (obj.empdetailes.to list ());

}

Desired to gain proficiency on ASP.Net?

Explore the blog post on ASP.Net training to become a pro in ASP.Net.

Step3: 

Creating view page for employee display:

Go to solution explorer

       ↓

Select the home folder from views

       ↓

Click with RM button

       ↓

Click on add

       ↓

Click on view

       ↓

Type the view page name (emp display)

       ↓

Note: Do not activate select master page

       ↓

Click on add

2 Go to source of emp display.aspx

       ↓

Write the following code

<%@ import name space = “examples.models”%>

<body>

<h2>satya technologies</h2>

<div>

<table border =“2” align = “center” >

<tr>

<th>Emp id </th><th>Ename</th><th>Designation</th><th>DOJ</th><th>salary</th><th>Deptno</th>

</tr>

<% for each (var x in (Enumerable<Emp detail>) model)

{

%>

<tr>

<td><%=HTML.Encode(x.Emp Id)%></td>

<td><%=HTML.Encode(x.Ename)%></td>

<td><%=HTML.Encode(x.designation)%></td>

<td><%=HTML.Encode(x.DOJ)%></td>

<td><%=HTML.Encode(x.salary)%></td>

<td><%=HTML.Encode(x.Deptno)%></td>

</tr>

<%}%>

</table>

</div>

</body>

 

Step4:

Creating view for adding a new record i.e. addnew.aspx

Go to solution explorer

       ↓

Select home folder from views

       ↓

Click on add

       ↓

Click on view

       ↓

Type the view name (add new)

       ↓

Activate the check box strongly typed view

       ↓

Select the view data class as

“example.models.emp detailes”

Uncheck the check box

Select the master page

Click on add

Go to source and write the following code

<h2> welcome to sathya technologies</h2>

<dir>

<%HTML.begin.form();%>

<Field set>

<table align=”center”border=”2”>

<tr>

<td><label for=”EmpId”>Enter EmpId</label></td>

<td><%=HTML.textbox(“EmpId”)%></td>

</tr>

<tr>

<td><Label for =”Ename”>enter Emp name</Label>

</td>

<td><%=HTML.textbox (“Empname”)%></td>

</tr>

<tr>

<td><label for =(”designation”)>enter designation</label>

</td>

<td><%=HTML.textbox(“Designetion”)%></td>

</tr>

<tr>

<td><label for= “DOJ”>enter DOJ</label></td>

<td><%=HTML.textbox (“DOJ”)%></td>

</tr>

<tr>

<td><label for= “salary”>enter salary</label></td>

<td><%=HTML.textbox (“salary”)%></td>

</tr>

<tr>

<td><label for= “Deptno”>enter Deptno</label></td>

<td><%=HTML.textbox (“Deptno”)%></td>

</tr>

<tr>

<td></td>

<td><input type=”submit” value=”save”></td>

</tr>

</field set>

<%HTML End form();%>

<%=HTML.actionlink (“back to Emp details ”,”Emp display”)%>

</div>

</body>

 

Step5:

Writing code for adds new action method:-

2Go to home controller.cs

       ↓

Write the following code for action methods

Public action result add new ()

{

Return value();

}

[Accept verbs (http verbs.post)]

Public action result add new([bind]Emp details.obj add)

{

Employee entities.obj=new employee entities ();

Obj.add to emp details (obj add);

Obj.save changes ();

Return redirect to action (“Emp display”);

}

Run the application end check

For indepth understanding click on