Raha
Raha

Reputation: 173

Display data from a table in a database to an ASP.net mvc 4 Web application

I am really new in ASP.Net MVC 4. I need to Display columns and their data in a grid -or webgrid or what- in a page of web application. I should implement the application by Asp.net MVC 4, Visual Studio 2012.

Any help or source to guide me?

Env. SQL server 2012 Visual Studio 2012

Upvotes: 1

Views: 8594

Answers (1)

user1987392
user1987392

Reputation: 3991

Considering that you have a data model based on that database (use Entity Framework), you can achieve that by using regular HTML tables, on your view (let's call it Table.cshtml):

@model IEnumerable<YourModelClass>

<!-- Using Regular HTML Tables -->
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstOrDefault().Property1)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FirstOrDefault().Property2)
    </th>
    (...)
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Property1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Property2)
        </td>
        (...)
    </tr>
}

In your YourModel controller you have something like:

public ActionResult Table()
{
    return View("Table", db.YourModel.ToList());
}

This is pretty simple, and is usually generated by scaffolding. I don't know if this fits your purpose.

This is a good tutorial for begginers: http://www.asp.net/mvc/tutorials/mvc-music-store

Other than that, there's plenty of good "grid" components around the Web with all sorts of "fancy features" like sorting/filtering/etc. (e.g.: I've used DevExpress).

Upvotes: 2

Related Questions