admin管理员组文章数量:1289565
I have a list of strings and the following code in cshtml
@foreach (string tag in Model.TagsList)
{
<li>@tag</li>
}
If I call my page without model I get the following exception Message=Object reference not set to an instance of an object.
How do I check if model is not null and if my list has values?
I have a list of strings and the following code in cshtml
@foreach (string tag in Model.TagsList)
{
<li>@tag</li>
}
If I call my page without model I get the following exception Message=Object reference not set to an instance of an object.
How do I check if model is not null and if my list has values?
Share Improve this question edited May 7, 2013 at 19:04 PSL 124k21 gold badges256 silver badges243 bronze badges asked May 7, 2013 at 18:59 BickBick 18.6k56 gold badges153 silver badges260 bronze badges1 Answer
Reset to default 8You can check like this:-
@if(Model != null && Model.TagsList != null) //NUll check for Model
{
foreach (string tag in Model.TagsList)
{
<li>@tag</li>
}
}
You don't need to check if TagsList
has values or not (if initialized) if empty List
it wont throw any error and won't step in to the loop.
本文标签: crazorcheck if parameter is null and list has argumentsStack Overflow
版权声明:本文标题:c# - razor - check if parameter is null and list has arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741479258a2381067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论