我在数据库中有这个表中未显示的工作表中的数据:MVC:如预期
我想在浏览器表中显示的数值(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>
这说明整个行的价值,而不仅仅是(ModuleID
和DateEntered
) 这是我在浏览器得到。
总结一下,我想获取表中的所有行,但只有特定的列。 目前情况下没有发生。 建议?
谢谢!那工作。但是ViewBag是一种传递数据的好方法来查看?如果我想显示来自另一个表的“ModuleTypeId”的名称,该怎么办? – Cybercop
强烈建议使用强类型的ViewModels,而不是使用ViewBag。你应该把你的模型作为参数传递给视图('return View(model);'),并在你的视图中使用'@model IEnumerable'(你可以从你的视图中删除'@ model',当你不使用它无论如何)。然后,您可以从'Model'属性的视图中访问模型:'@foreach(Model.Entries中的var条目)' –
tdragon
否View Bag不好的方法在Models中创建Model和绑定值。使用模型显示值。 – Amit