Display data in a table on a view in MVC

Hi,

In this below example i was fetching the data from DB using Entity framework and display it on the view

Code Inside controller :

public class EFSampleController : Controller
{
public ActionResult Index()
{
         SampleMVCDBContext dbContextObj = new SampleMVCDBContext();
                    List<AccountInfo> list = dbContextObj.AccountInfoes.ToList();

// In the above we are fetching data from DB , here you can fetch from either Business Access Layers or by using  any               // other data sources
return View(list);
}
}

Code Inside View :

@model List<EFAccess.AccountInfo>
@{
ViewBag.Title = “Index”;
}
<h2>
Demo With JQuery Datatables</h2>
<table id=”tblDemo”>
<thead>
<tr>
<th>
Name
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Phone
</th>
<th>
UserName
</th>

</tr>
</thead>
<tbody>
@foreach (var obj in Model)
{
<tr>
<td>
@obj.Name
</td>
<td>
@obj.City
</td>
<td>
@obj.State
</td>
<td>
@obj.Phone
</td>
<td>
@obj.UserName
</td>

</tr>
}
</tbody>
</table>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s