如何使用ASP.net MVC从数据库中获取数据?

时间:2016-06-03 06:19:40

标签: c# sql asp.net-mvc linq-to-sql

这是我的看法。



<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>
&#13;
&#13;
&#13;

我已经通过创建一个表在mvc中创建了一个视图。我的目标是用sql数据库中的数据填充表。我的控制器看起来像这样

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

在模型中,我不知道如何使用LINQ to SQL查询

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

但我不知道如何做剩余的步骤。代码是用模型编写的吗?

1 个答案:

答案 0 :(得分:0)

  1. 您必须将此数据发送到视图。更改索引操作,如下所示

    public ActionResult Index()
    {
      OnlineModel model = new OnlineModel();
      var result = model.GetAdminData();
      return View(result);
    }
    
  2. 并修改了您的视图以获取此数据。

    @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>