Model view controller MVC和Razor在值为空或0时显示整数
我在视图中有以下代码Model view controller MVC和Razor在值为空或0时显示整数,model-view-controller,Model View Controller,我在视图中有以下代码 <span>@Html.DisplayFor(modelItem => item.comments.comments_id)</span> <span>@item.comments.comments_id</span> 如何克服这个问题?提前谢谢 ----编辑更多代码--- 看法 @foreach(模型中的变量项) { @DisplayFor(modelItem=>item.ProjectName) @Display
<span>@Html.DisplayFor(modelItem => item.comments.comments_id)</span>
<span>@item.comments.comments_id</span>
如何克服这个问题?提前谢谢
----编辑更多代码---
看法
@foreach(模型中的变量项)
{
@DisplayFor(modelItem=>item.ProjectName)
@DisplayFor(modeleItem=>item.Comments.Comments\u id)//注意这是来自另一个表(Comments-tbl)。
@((item.Comments.Comments\u id==0)?0:@item.Comments.Comments\u id)//此处当item.Comments.Comments\u id没有内容时,将抛出错误。
}
有人贴了类似的东西,但我不明白答案。
您需要使用HasValue检查可为空的变量是否有值
<span>@((item.comments.comments_id.HasValue) ? @item.comments.comments_id : 0)</span>
@((item.comments.comments\u id.HasValue)?@item.comments.comments\u id:0)
编辑:添加示例
您的视图代码看起来应该可以工作。下面是我为测试您的视图而编写的代码。控制器操作中的lambda表达式在使用之前不会执行。因此,在渲染视图之前,它可能不会执行。这可能会导致问题,因为此时数据库连接可能已关闭。但是,对ToList()的调用应该执行lambda表达式。所以问题可能是item.Comments为null。您应该进行测试,看看它是否像我在示例中所做的那样
控制器
public class HomeController : Controller
{
public class Project
{
public string ProjectName { get; set; }
public ProjectComments Comments { get; set; }
}
public class ProjectComments
{
public string Comments { get; set; }
public Nullable<int> comments_id { get; set; }
}
public ActionResult Index()
{
var showItems = new List<Project>();
showItems.Add(new Project()
{
ProjectName = "Project 1",
Comments = new ProjectComments() { Comments = "some comments", comments_id = 1 }
});
showItems.Add(new Project()
{
ProjectName = "Project 2",
Comments = new ProjectComments() { Comments = "more comments", comments_id = null }
});
return View(showItems);
}
}
公共类HomeController:控制器
{
公共类项目
{
公共字符串ProjectName{get;set;}
公共项目注释{get;set;}
}
公共类项目评论
{
公共字符串注释{get;set;}
公共可空注释\u id{get;set;}
}
公共行动结果索引()
{
var showItems=新列表();
添加(新项目()
{
ProjectName=“项目1”,
Comments=newprojectcomments(){Comments=“some Comments”,Comments\u id=1}
});
添加(新项目()
{
ProjectName=“项目2”,
Comments=newprojectcomments(){Comments=“more Comments”,Comments\u id=null}
});
返回视图(显示项目);
}
}
查看
@model IList<MvcApplication2.Controllers.HomeController.Project>
<table border="1">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments.comments_id)
</td>
<td>
<span>
@((item.Comments != null && item.Comments.comments_id.HasValue) ? @item.Comments.comments_id : 0)
<a href="#">Create</a>
</span>
</td>
</tr>
}
</table>
@model-IList
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.ProjectName)
@DisplayFor(modelItem=>item.Comments.Comments\u id)
@((item.Comments!=null&&item.Comments.Comments\u id.HasValue)?@item.Comments.Comments\u id:0)
}
您需要使用HasValue检查可为空的变量是否有值
<span>@((item.comments.comments_id.HasValue) ? @item.comments.comments_id : 0)</span>
@((item.comments.comments\u id.HasValue)?@item.comments.comments\u id:0)
编辑:添加示例
您的视图代码看起来应该可以工作。下面是我为测试您的视图而编写的代码。控制器操作中的lambda表达式在使用之前不会执行。因此,在渲染视图之前,它可能不会执行。这可能会导致问题,因为此时数据库连接可能已关闭。但是,对ToList()的调用应该执行lambda表达式。所以问题可能是item.Comments为null。您应该进行测试,看看它是否像我在示例中所做的那样
控制器
public class HomeController : Controller
{
public class Project
{
public string ProjectName { get; set; }
public ProjectComments Comments { get; set; }
}
public class ProjectComments
{
public string Comments { get; set; }
public Nullable<int> comments_id { get; set; }
}
public ActionResult Index()
{
var showItems = new List<Project>();
showItems.Add(new Project()
{
ProjectName = "Project 1",
Comments = new ProjectComments() { Comments = "some comments", comments_id = 1 }
});
showItems.Add(new Project()
{
ProjectName = "Project 2",
Comments = new ProjectComments() { Comments = "more comments", comments_id = null }
});
return View(showItems);
}
}
公共类HomeController:控制器
{
公共类项目
{
公共字符串ProjectName{get;set;}
公共项目注释{get;set;}
}
公共类项目评论
{
公共字符串注释{get;set;}
公共可空注释\u id{get;set;}
}
公共行动结果索引()
{
var showItems=新列表();
添加(新项目()
{
ProjectName=“项目1”,
Comments=newprojectcomments(){Comments=“some Comments”,Comments\u id=1}
});
添加(新项目()
{
ProjectName=“项目2”,
Comments=newprojectcomments(){Comments=“more Comments”,Comments\u id=null}
});
返回视图(显示项目);
}
}
查看
@model IList<MvcApplication2.Controllers.HomeController.Project>
<table border="1">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments.comments_id)
</td>
<td>
<span>
@((item.Comments != null && item.Comments.comments_id.HasValue) ? @item.Comments.comments_id : 0)
<a href="#">Create</a>
</span>
</td>
</tr>
}
</table>
@model-IList
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.ProjectName)
@DisplayFor(modelItem=>item.Comments.Comments\u id)
@((item.Comments!=null&&item.Comments.Comments\u id.HasValue)?@item.Comments.Comments\u id:0)
}
尝试了一下,我也遇到了同样的错误System.NullReferenceException:对象引用未设置为对象的实例
则item或item.comments为null(假设这是引发异常的行)。你必须发布更多的代码,这样我们才能看到发生了什么。我在上面添加了一些示例代码来帮助你定位问题。Scott。感谢您花时间编写测试代码。该项正确。注释为空。我想知道我是否可以在控制器上操作代码,并为空项设置一个标志。尝试了之后,我得到了相同的错误System.NullReferenceException:对象引用未设置为对象的实例
则item或item.comments为null(假设这是引发异常的行)。你必须发布更多的代码,这样我们才能看到发生了什么。我在上面添加了一些示例代码来帮助你定位问题。Scott。感谢您花时间编写测试代码。该项正确。注释为空。我想知道是否可以在控制器上操作代码并为空项设置标志。
@model IList<MvcApplication2.Controllers.HomeController.Project>
<table border="1">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments.comments_id)
</td>
<td>
<span>
@((item.Comments != null && item.Comments.comments_id.HasValue) ? @item.Comments.comments_id : 0)
<a href="#">Create</a>
</span>
</td>
</tr>
}
</table>