0

我的观点是使用 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);
4

3 回答 3

2

您可以发送列表操作 emmty 列表示例:

public ActionResult List()
{
//hear check if is null 
return View(new List<yourModel>());
}
于 2011-03-20T17:31:50.627 回答
1
  public static class YourWelcome{
     public static bool IsNullOrHasNullProperties(this object model){
         if(model == null) { 
             return true;
         }
         return model.GetType()
              .GetProperties(BindingFlags.Public|BindingFlags.Instance)
              .Where(propertyInfo => !propertyInfo.PropertyType.IsValueType)
              .Any(propertyInfo => propertyInfo.GetValue(model,null) == null);
     }
     public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable){
         return enumerable == null || !enumerable.Any();
     }
  }
于 2011-03-20T17:34:05.110 回答
1

当模型没有任何数据时,这就是我使用 webgrid 处理场景的方式。

if (Model.Results.Count() > 0)
 {

     <div id="grid">


        @grid.GetHtml()

     </div> 
}
else
{
    <p>No Results Found.</p>
}
于 2011-04-19T02:51:41.777 回答