2013-06-20 21 views
0

我在数据库中有这个表中未显示的工作表中的数据:MVC:如预期

enter image description here

我想在浏览器表中显示的数值(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) 这是我在浏览器Browser output得到。

总结一下,我想获取表中的所有行,但只有特定的列。 目前情况下没有发生。 建议?

回答

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

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

应该看起来更像是这样的:

@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> 
} 
+0

谢谢!那工作。但是ViewBag是一种传递数据的好方法来查看?如果我想显示来自另一个表的“ModuleTypeId”的名称,该怎么办? – Cybercop

+0

强烈建议使用强类型的ViewModels,而不是使用ViewBag。你应该把你的模型作为参数传递给视图('return View(model);'),并在你的视图中使用'@model IEnumerable '(你可以从你的视图中删除'@ model',当你不使用它无论如何)。然后,您可以从'Model'属性的视图中访问模型:'@foreach(Model.Entries中的var条目)' – tdragon

+0

否View Bag不好的方法在Models中创建Model和绑定值。使用模型显示值。 – Amit