0

我在数据库中有这个表:

在此处输入图像描述

我正在尝试将浏览器中的值(ModuleIDDateEntered)显示为表格。我正在使用 viewbag 将值传递给我的视图,这不是正确的方法,因为我现在只得到一行。我现在正在做的是

public ActionResult Index()
        {
            var modules = (from entries in _db.Modules
                           orderby entries.DateEntered
                           select entries);
            ViewBag.entries = modules.ToList();
            return View();
        }

如何从上图中的表格中获取所有行并将其传递给查看?在我看来,我目前有:

@using BootstrapSupport

    @model Sorama.DataModel.SIS.ModuleStock.Module

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
    }

    <table class="table table-striped">
        <caption></caption>
        <thead>
            <tr>
                <th>
                   Module ID
                </th>

                <th>
                    Date Entered
                </th>
            </tr>
        </thead>
        <tr>
             @foreach (var entry in ViewBag.entries)
            {
                <td>@entry.ModuleId</td>
                 <td>@entry.DateEntered</td>
            }
             <td>
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                            Action
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                                <li>@Html.ActionLink("Details", "Details")</li>
                                @if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Admin"))
                                {
                                    <li class="divider"></li>
                                    <li>@Html.ActionLink("Edit", "Edit")</li>
                                    <li>@Html.ActionLink("Delete", "Delete")</li>
                                }



                        </ul>
                    </div>

                </td>
        </tr>

    </table>

这显示了整行的值,而不仅仅是 (ModuleIDDateEntered) 这是我在浏览器中得到的浏览器输出

总而言之,我想从表中获取所有行,但只获取特定列。在当前情况下不会发生这种情况。建议?

4

2 回答 2

2

你误读了你的结果。有 3 条记录,每行显示 2 个字段。您的视图中有单个,并从标签中<tr>插入值。您应该为您的每条记录添加新的。ViewBag<td><tr>ViewBag

它应该看起来更像这样:

@foreach (var entry in ViewBag.entries)
{
    <tr>
        <td>@entry.ModuleId</td>
        <td>@entry.DateEntered</td>
        <td>
            <div class="btn-group">
                <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                    Action<span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li>@Html.ActionLink("Details", "Details")</li>
                    @if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Admin"))
                    {
                        <li class="divider"></li>
                        <li>@Html.ActionLink("Edit", "Edit")</li>
                        <li>@Html.ActionLink("Delete", "Delete")</li>
                    }       

                </ul>
            </div>    
        </td>
    </tr>
}
于 2013-06-20T09:49:16.517 回答
1

试试这个

 public ActionResult Index()
    {
        ABCList abc=new ABCList();
        var modules = (from entries in _db.Modules
                       orderby entries.DateEntered
                       select new ABC {
                           id=entries.id,
                           ModuleTypeId=entries.ModuleTypeId,
                           ModuleId=entries.ModuleId,
                           DataEntered=entries.DataEntered
                        });
        abc.settings = modules.ToList();
        return View();
    }

  public class ABC
  {

    public long Id{ get; set; }
    public long ModuleTypeId{ get; set; }
    public string ModuleId{get;set;}
    public DateTime DataEntered{ get; set; }
  }

  public class ABCList{
    public List<ABC> Settings { get; set; }
  }

看法

  @model ABCList
  @foreach (var entry in Model.Settings)
 {
 <tr>
    <td>@entry.ModuleId</td>
    <td>@entry.DateEntered</td>
    <td>
        <div class="btn-group">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                Action<span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                <li>@Html.ActionLink("Details", "Details")</li>
                @if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Admin"))
                {
                    <li class="divider"></li>
                    <li>@Html.ActionLink("Edit", "Edit")</li>
                    <li>@Html.ActionLink("Delete", "Delete")</li>
                }       

            </ul>
        </div>    
    </td>
 </tr>
}
于 2013-06-20T10:16:34.503 回答