'How to fetch data from database using ASP.net MVC?


This is my view.

<body>
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid" style="font-size:13.9px;font-family:'Segoe UI';">
            <div>

                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist" style="padding:0px;padding-top:10px">
                    <li role="presentation" class="activeIndex"><a href="#home" aria-controls="home" role="tab" data-toggle="tab" >EventLog</a></li>
                    <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">ApplicationLog</a></li>
                    
                </ul>

                <!-- Tab panes -->
                <div class="tab-content" style="padding-top:15px">
                    <div role="tabpanel" class="tab-pane active" id="home">
                        <table class="table table-striped ">
                            <thead style="background-color: rgba(7,45,120,.8); color: white; font-size: initial;">
                                <tr>

                                    <th>EventLogID</th>
                                    <th>UserName</th>
                                    <th>Message</th>
                                    <th>Severity</th>
                                    <th>Type</th>
                                    <th>StackTrace</th>
                                    <th>PageSource</th>
                                    <th>CreatedDttm</th>

                                </tr>
                            </thead>

                        </table>
                    </div>
                    <div role="tabpanel" class="tab-pane" id="profile">
                        <table class="table table-striped">
                            <thead style="background-color: rgba(7,45,120,.8); color: white; font-size: initial">
                                <tr>

                                    <th>EventLogID</th>
                                    <th>UserName</th>
                                    <th>UserValidationMessage</th>
                                    <th>DetailedMessage</th>
                                    <th>LogInTime</th>
                                    <th>LogOutTime</th>
                                    <th>CreatedDttm</th>


                                </tr>
                            </thead>

                        </table>



                    

            </div>
        </div><!-- /.container-fluid -->
    </nav>

I have created a view in mvc by creating a table.My aim is to populate the table with the data in the sql database.My Controller looks something like this

public ActionResult Index()
{
    OnlineModel model = new OnlineModel();
     var result = model.GetAdminData();
    return View();
}

And in model I don't know how to use the LINQ to SQL query

public string GetAdminData()
{
    var result = ((from eng in odsEntities.EventLogs select eng.EventLogID).Distinct().ToList());
    return "";
}

But I don't know how to do the remaining steps. Is the code written in model correct ?



Solution 1:[1]

  1. You must send this data to the view. Change your index action as follow

    public ActionResult Index()
    {
      OnlineModel model = new OnlineModel();
      var result = model.GetAdminData();
      return View(result);
    }
    
  2. And modified your view for getting this data.

    @model IEnumerable<EventLog>// write it to head of page Perhaps you need to add true namespace for Eventlog class
    // then write it inside table
    <tbody>
        @foreach (item in Model)
        {
            <tr>
                <td>@item.EventLogID</td>
                <td>@item.UserName</td>
                <td>@item.Message</td>
                <td>@item.Severity</td>
                <td>@item.Type</td>
                <td>@item.StackTrace</td>
                <td>@item.PageSource</td>
                <td>@item.CreatedDttm</td>
            </tr>
        }
    </tbody>
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elvin Mammadov