我的观点是使用 WebGrid 来显示一个网格。
如果模型有一些数据,它会很好,但如果模型没有数据,那么 WebGrid 会给出异常。
我尝试检查是否Model != null
,但这只是让我在 IF 中的代码执行。还尝试进行检查以查看if (Model.Count() > 2)
,这只是给了我一条消息说"The specified resource does not exist."
。
在这两个条件中的任何一个条件下,IF 内部的代码都会执行。有没有一种简单的方法可以检查传入的信息以查看模型是否有任何行?
@model IEnumerable<Selftestware.Storage.Models.TestFormat>
@section content {
<div id="content">
@if ( Model != null) {
var grid = new WebGrid(
source: Model,
defaultSort: "Name",
canPage: true,
canSort: true,
rowsPerPage: 20);