【问题标题】:How to fetch data from database using ASP.net MVC?如何使用 ASP.net MVC 从数据库中获取数据?
【发布时间】:2016-06-03 06:19:39
【问题描述】:

这是我的看法。

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

我通过创建表在 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 "";
}

但我不知道如何完成剩下的步骤。用model写的代码是否正确?

【问题讨论】:

  • 您的视图是否使用模型强类型化?
  • 不,它不是强类型@Katana
  • 我为您查找了一篇文章asp.net/mvc/overview/older-versions-1/models-data/… 花点时间阅读。为了降低复杂性,使您的视图具有强类型,以便您可以访问视图中的模型。
  • 谢谢@Katana 我需要一些关于 LINQ to SQL 的说明
  • 首先您的查询是创建List&lt;int&gt;(假设EventLogIDint 的类型,因此该方法需要是public List&lt;int&gt; GetAdminData()(不是string)及其return result; 然后控制器方法需要向视图返回一些东西。但是你已经创建了一个OnlineModel然后把它扔掉了,你已经创建了一个List&lt;int&gt;并且把它也扔掉了所以不清楚你想要返回什么到视图。

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


【解决方案1】:
  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>
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 2016-12-02
    • 2017-09-20
    • 1970-01-01
    • 2014-08-19
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多