Asp.net mvc MVC:显示未按预期工作的表数据
我在数据库中有这个表: 我试图在浏览器中将值(Asp.net mvc MVC:显示未按预期工作的表数据,asp.net-mvc,linq,asp.net-mvc-4,html-table,viewbag,Asp.net Mvc,Linq,Asp.net Mvc 4,Html Table,Viewbag,我在数据库中有这个表: 我试图在浏览器中将值(ModuleID和DateEntered)显示为表。 我使用viewbag将值传递给我的视图,这不是很正确的方法,因为我现在只得到一行。我现在正在做的是 public ActionResult Index() { var modules = (from entries in _db.Modules orderby entries.DateEntered
ModuleID
和DateEntered
)显示为表。
我使用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>
@使用BootstrapSupport
@模型Sorama.DataModel.SIS.ModuleStock.Module
@{
ViewBag.Title=“Index”;
Layout=“~/Views/shared/\u BootstrapLayout.basic.cshtml”;
}
模块ID
输入日期
@foreach(ViewBag.entries中的var条目)
{
@entry.ModuleId
@entry.DateEntered
}
- @ActionLink(“详细信息”、“详细信息”)
@if(Request.IsAuthenticated&&HttpContext.Current.User.IsInRole(“Admin”))
{
- @ActionLink(“编辑”、“编辑”)
- @ActionLink(“删除”、“删除”)
}
这将显示整行的值,而不仅仅是(ModuleID
和DateEntered
)
这是我在浏览器中得到的
总之,我想从表中获取所有行,但只获取特定列。
这在目前的情况下是不会发生的。
建议?你误读了你的结果。有3条记录,每行显示2个字段。您的视图中只有一个
,您可以在
标记中插入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>
}
@foreach(ViewBag.entries中的变量条目)
{
@entry.ModuleId
@entry.DateEntered
- @ActionLink(“详细信息”、“详细信息”)
@if(Request.IsAuthenticated&&HttpContext.Current.User.IsInRole(“Admin”))
{
- @ActionLink(“编辑”、“编辑”)
- @ActionLink(“删除”、“删除”)
}
}
您误读了结果。有3条记录,每行显示2个字段。您的视图中只有一个
,您可以在
标记中插入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>
}
@foreach(ViewBag.entries中的变量条目)
{
@entry.ModuleId
@entry.DateEntered
- @ActionLink(“详细信息”、“详细信息”)
@if(Request.IsAuthenticated&&HttpContext.Current.User.IsInRole(“Admin”))
{
- @ActionLink(“编辑”、“编辑”)
- @ActionLink(“删除”、“删除”)
}
}
试试这个
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; }
}
public ActionResult Index()
{
ABCList abc=新的ABCList();
var modules=(来自_db.modules中的条目
orderby entries.DateEntered
选择新ABC{
id=entries.id,
ModuleTypeId=entries.ModuleTypeId,
ModuleId=entries.ModuleId,
DataEntered=条目。DataEntered
});
abc.settings=modules.ToList();
返回视图();
}
公共课ABC
{
公共长Id{get;set;}
公共长ModuleTypeId{get;set;}
公共字符串ModuleId{get;set;}
公共日期时间数据输入{get;set;}
}
公共类ABCList{
公开名单
- @ActionLink(“详细信息”、“详细信息”)
@if(Request.IsAuthenticated&&HttpContext.Current.User.IsInRole(“Admin”))
{
- @ActionLink(“编辑”、“编辑”)
- @ActionLink(“删除”、“删除”)
}
}
试试这个
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; }
}
public ActionResult Index()
{
ABCList abc=新的ABCList();
var modules=(来自_db.modules中的条目
orderby entries.DateEntered
选择新ABC{
id=entries.id,
ModuleTypeId=entries.ModuleTypeId,
ModuleId=entries.ModuleId,
DataEntered=条目。DataEntered
});
abc.settings=modules.ToList();
返回视图();
}
公共课ABC
{
公共长Id{get;set;}
公共长ModuleTypeId{get;set;}
公共字符串ModuleId{get;set;}
公共日期时间数据输入{get;set;}
}
公共类ABCList{
公开名单
- @Html.A