我想在 SQL 数据库中使用 3 个表在 GridView 中显示数据。
首先我创建了模型
public class common
{
public Artist Artist { get; set; }
public Album Album { get; set; }
public Genre Genre { get; set; }
}
那么这是 Controller
public ActionResult Show1()
{
var query = from a in DB.Album
join b in DB.Artists
on a.ArtistId equals b.ArtistId
join c in DB.Genre
on a.GenreId equals c.GenreId
where (b.ArtistId == 2)
select new common { Album = a, Artist = b, Genre = c };
return View(query.ToList());
}
}
之后这是我的 View
@model IEnumerable<test1.Models.common>
@{
ViewBag.Title = "Show1";
}
<h2>Show1</h2>
<div>
@{
var grid = new WebGrid(Model, defaultSort:"Name");
}
@grid.GetHtml()
</div>
但是没有显示任何数据? 我该怎么做?
最佳答案
我认为您需要一个用于通用对象模型的编辑器模板 或者使用 for 语句并填充 html 表
例如...
<table summary="">
<thead>
<tr>
<th></th>
<th>
Account No.
</th>
<th>
Customer Name
</th>
<th class="SingleCheckBox">
Is Approved
</th>
<th class="SingleCheckBox">
Is Locked out
</th>
<th>
Last Login
</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Count(); ++i)
{
var item = Model[i];
bool isSelected = item.AccountNo == selectedAccountNo;
<tr>
<td>@Html.RadioButton("selectedUserName", item.UserName, isSelected, new { name = "selectedUserName" })</td>
<td>
@Html.DisplayFor(model => model[i].UserName)
@Html.HiddenFor(model => model[i].UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td class="SingleCheckBox">
@Html.CheckBoxFor(model => model[i].IsApproved)
</td>
<td class="SingleCheckBox">
@if (item.IsLockedOut)
{
@Html.CheckBoxFor(model => model[i].IsLockedOut);
}
else
{
@Html.CheckBoxFor(model => model[i].IsLockedOut);
}
</td>
<td class="last-child">
@(TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(item.LastLoginDate, DateTimeKind.Utc), timeZoneInfo).ToString())
</td>
</tr>
}
</tbody>
</table>
关于c# - 在 Gridview 中显示来自 SQL 数据库的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311626/