How to work with MVC models with an example
Create a new MVC2 application with the name “examples”;
Go to solution explorer
↓
Select the models folder
↓
Click with RM button
↓
Click on add
↓
Click on new item
↓
Select data option from visual C# in installed template
↓
S elect entity data model
↓
Type the EDM name (employee.EOMX)
↓
Click on add
↓
Select the option generate from data base
↓
Click on next
↓
Click on new connection
↓
Type the server name (“dot net” or satya)
↓
Activate the radio button
↓
Use SQL authentication
↓
--> Type username (“Sa”);
↓
Type password (“ABC”);
↓
Select the D/B as employee
↓
Click on ok
↓
Active the radio button --> yes includes the sensitive data in the connection string
↓
Connection string name you find as employee entity
↓
Click on next
This will retrieve complete D/B objects and
↓
From select emp details table
↓
Click on finish
- Employee entities
- Emp details
The model for data is ready
Learn the core features of ASP.NET Training and become master with our expertise tutorials.
Creation action method in control class
↓
Go to home controller.cs file
↓
Write the following code
Using examples models; name space
Public action result sample data ()
{
Employee entities.obj=new employee entities ();
View data [“Emp”] = obj.Emp details to list();
Return views ();
}
Creating view:
Go to home folder;
↓
Add new view page (sample data)
↓
Write the following code
<%@ import name space = “examples.models”%>
<body>
<h1>welcome to satya</h1>
<div>
<table align = “center” border = “2”>
<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 class detail>) view data [“Emp”]
{ // C# code
%>
<tr>
<td><%=HTML.Encode(x.Emp Id)%></td>
<td><%=HTML.Encode(x.Ename)%></td>
<td><%=HTML.Encode(x.designation)%></td>
<td><%=HTML.Encode(DOJ)%></td> // MVC code
‘
‘
</tr>
<%}%> //C# code
</table>
</div>
</body>
Run the application and check
For indepth understanding click on