0

I have created a DataTable from a SQL query in the the controller.

How do I "connect" '#example' with 'categories'?

EDIT1: After suggestion to use jQuery.DataTables

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Index Page</title>
</head>
<body>
    <div>
    <h1>Employee Index Page</h1>
        <table id="example">

</div>
</body>
</html>

@{ var categories = (System.Data.DataTable)ViewData["MyData"]; }

<script>
    $(document).ready(function () {
        $('#example').DataTable();
    });
</script>

ORIGINAL

I want to assign this to a gridview. The Code from the View is below but GridView1 needs to be defined somehow.

In the c# categories has the right contents, but GridView1 get the error "does not exist in the current context".

How and where do I fix that?

    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Employee Index Page</title>
    </head>
    <body>
        <div>
        <h1>Employee Index Page</h1>
            <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                          runat="server" DataSource='<%# GetData() %>' AutoGenerateColumns="true">
            </asp:GridView>
            @{ 
                var categories = (System.Data.DataTable)ViewData["MyData"];
                GridView1.DataSource = categories;
                GridView1.DataBind();
            }
        </div>
    </body>
    </html>
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • Why are you using asp:gridview in MVC? – ChrisBint Oct 28 '16 at 08:39
  • Web search suggested it was a simple way to display a DataTable. I am open to alternatives! – ManInMoon Oct 28 '16 at 08:41
  • Personally, I would look at using datatables (or similar) with MVC http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part – ChrisBint Oct 28 '16 at 08:42
  • You can't mix ASP controls and MVC I am pretty sure (honestly I have never thought to try...) – Michael Coxon Oct 28 '16 at 08:47
  • You can just do a normal HTML `` and for-loop over all the headers, columns, rows, etc...
    – Michael Coxon Oct 28 '16 at 08:48
  • @MichaelCoxon So JQuery.DataTables is a good solution? – ManInMoon Oct 28 '16 at 08:50
  • I am using : http://www.c-sharpcorner.com/uploadfile/4d9083/creating-simple-grid-in-mvc-using-grid-mvc/ – Sunil Kumar Oct 28 '16 at 08:50
  • jQuery DataTables is just a JS plugin for formatting an already built HTML `` it adds client side filtering, paging and junk. You still need to write out all the HTML manually.
    – Michael Coxon Oct 28 '16 at 08:52
  • See: http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc Keep in mind that this is just a quick fix for the way you have already set up your model. But honestly, I would seriously recommend that you change your model to be an `IEnumerable` since it is so much easier to read and seperates the view model from the domain model - which is typically what you want to do in MVC. – Michael Coxon Oct 28 '16 at 08:53
  • @ChrisBint I have made changes to the question - how do I connect jquery.datatable with my datatable? – ManInMoon Oct 28 '16 at 10:56

1 Answers1

0

First, I recommend to you not to put everyrthing in one view, Create View for Grid take @model List<YourClass> and View for your Layout.

For Display GridView in MVC, There are many options (Free and not-free)

DataTable Example

your Html for example
---------------------
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            <tr>
                <td>@item.Name</td>
                <td>@item.Position</td>
            </tr>
         <tbody>
    </table>


<script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.12.3.js"></script> 
<script type="text/javascript" charset="utf8"  src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
 <script> 
      $(document).ready(function () { 
         $('#example').DataTable(); 
       }); 
 </script> 
Ahmed
  • 1,542
  • 2
  • 13
  • 21
  • Ahmed, I have made changes to the question - how do I connect jquery.datatable with my datatable? – ManInMoon Oct 28 '16 at 10:55
  • you need to unserstand more about MVC first and javascript – Ahmed Oct 28 '16 at 10:58
  • this is an opensource project I created before using MVC, javscript, Ajax ,, i think it will help https://github.com/AhmedRagheb/Movies-Search – Ahmed Oct 28 '16 at 10:58
  • @ManInMoon I Edit my answer with example to how use DataTable – Ahmed Oct 28 '16 at 11:08
  • Ahmed, thank you for code above. I am confused - why do you need to loop through and create rows when using JQuery.Datatable? I might as well just write the HTML? I thought point of Jquery was to make it simpler? – ManInMoon Oct 28 '16 at 11:15
  • Jquery DataTable helper library convert your table to GridView with some functionality like search, sort ... my example I fill table using MVC razor but you can fill it using Ajax or anything else .. you got it !! :) – Ahmed Oct 28 '16 at 11:17
  • Ahmed, I have that all working EXCEPT the Javascript - where do I put that? Currently it is under my HTML body. I don't think it gets called – ManInMoon Oct 28 '16 at 12:00
  • Did you add the script file ? – Ahmed Oct 28 '16 at 12:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126909/discussion-between-maninmoon-and-ahmed-ragheb). – ManInMoon Oct 28 '16 at 12:08
  • @ManInMoon If this answer your question, solved your problem and you satisifed with answer, could you mark it as solved to help other who search – Ahmed Oct 28 '16 at 14:20
  • For others reading this - a key point is to remove layout=null in the view. – ManInMoon Oct 29 '16 at 07:54