简体   繁体   English

在视图中显示模型数据,检查是否为空值

[英]Displaying model data in view, check for null value

I'm not sure if this is possible in the view or if I would somehow have to check for it in the controller but I have some values that are null and I'm trying to fill up a table but I would like a placeholder such as N/A to show up if there's no values. 我不确定在视图中是否可行,或者我是否必须以某种方式在控制器中进行检查,但是我有一些值为null的值,并且正在尝试填充表,但是我想要一个这样的占位符如果没有值,则显示为N / A。

In my controller I'm just return the model data of a basic linq query. 在我的控制器中,我只是返回基本linq查询的模型数据。

var model = from u in db.Users where u.Username == "Bob" select u;

In my view I'm just simply displaying the data in a table 在我看来,我只是在表格中显示数据

<table class='table table-striped table-bordered table-responsive'>
    <tbody>
        <tr>
            <td>@Html.DisplayFor(model => model.CallNo)</td>
        </tr>
    </tbody>
</table>

you can do something like this in your model: 您可以在模型中执行以下操作:

[DisplayFormat(NullDisplayText = "PLACEHOLDER_VALUE")]
public string CallNo { get; set; }

Doing this you can just use 这样做,您可以使用

@Html.DisplayFor(model => model.CallNo) @ Html.DisplayFor(model => model.CallNo)

as usual. 照常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM