[英]Check for null value of model property inside partial view
I am working on a asp.net mvc 3 application and I've made several partial views each one responsible for rendering of specific logic. 我正在一个asp.net mvc 3应用程序上工作,我做了几个局部视图,每个局部视图负责呈现特定的逻辑。 Inside one of my views I use properties which can be null, but I don't want to pass null to the
@Html.DisplayFor()
and write something more user friendly to the user to know that these fields are not missing, they just don't have nothing assigned to them yet. 在我的一个视图中,我使用了可以为null的属性,但我不想将null传递给
@Html.DisplayFor()
并向用户写一些对用户更友好的信息,以知道这些字段没有丢失,它们只是尚未分配任何东西。
So I try this : 所以我尝试这样:
<tr>
<td>
@if (!string.IsNullOrEmpty(Model[0][0].FieldValue))
{
@Html.DisplayFor(Model => Model[0][0].FieldValue)
}
</td>
<td>
@Html.DisplayFor(Model => Model[1][0].FieldValue)
</td>
</tr>
I don't have else
clause because writing the if
statement results in getting both Model =>
in the DisplayFor
marked with red and the following message : 我没有
else
子句,因为编写if
语句会导致在DisplayFor
中获得用红色标记的Model =>
和以下消息:
A local variable named 'Model' can not be declared in this scope because it would give a different meaning to 'Model' which is already used in a 'parent or current' scope to denote something.
不能在此范围内声明名为“ Model”的局部变量,因为它将给“ Model”赋予不同的含义,“ Model”已在“父级或当前”范围中用于表示某些内容。
Basically I think I understand what this error means however I don't know how to check for null properly in this situation. 基本上,我认为我理解此错误的含义,但是在这种情况下,我不知道如何正确检查null。
The error message is caused by the redefinition of the Model variable. 该错误消息是由Model变量的重新定义引起的。 Try
尝试
@Html.DisplayFor(x => x[0][0].FieldValue)
You might find this SO question useful to understand the "=>" thingie. 您可能会发现此SO问题对于理解“ =>”事物很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.