【发布时间】:2021-12-20 17:35:57
【问题描述】:
我正在学习 ASP.NET MVC 5,并且正在学习如何使用我使用数据库创建的数据填充表。我看到数据填充得很好(见下图),但第二列没有正确填充。
我不知道如何设置表格的样式,以免发生这种情况。这就是我需要你帮助的地方。
这就是我的 Index.cshtml:
@model IEnumerable<Vidly.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
<div class="table-responsive">
<table id="customers" class="table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Discount Rate</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name, "Details", "Customers", new {id = customer.Id}, new {@class = ""})<td>
<td>@customer.MembershipTypes.DiscountRate<td>
</tr>
}
</tbody>
</table>
</div>
<script>
$('#customers tr').each(function() {
if ($(this).children('td:empty').length === $(this).children('td').length) {
$(this).remove(); // or $(this).hide();
}
});
</script>
这是 _Layout.cshtml,其中“部分视图”被引用:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
@Html.Partial("_NavBar")
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - Vidly</p>
</footer>
</div>
@Scripts.Render("~/bundles/lib")
@RenderSection("scripts", required: false)
</body>
</html>
如果我缺少任何东西,请告诉我,我将提供帮助解决此问题所需的所有反馈。非常感谢。
【问题讨论】:
-
你用另一个
结束 foreach 循环中的第一个 ,而不是 。 -
@VDWWD 感谢您的及时回复。请参阅我更新的消息。进行更改后,我的表中多了一些空的
。想法? 同样的问题,你还在用<td>而不是</td>@RoryMcCrossan 你说的太对了。看我的回答
标签: html jquery css asp.net razor