检查是否DateTime类型的值是针对空,如果空显示空白 [英] Checking if the value of type DateTime is null in view and showing blank if null

查看:322
本文介绍了检查是否DateTime类型的值是针对空,如果空显示空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的控制器我已经过了价值模块来查看

 公共ActionResult的详细信息(长ID,串所有者)
    {
        VAR模块= _ownedModuleRepository.GetModuleDetails(ID,所有者);        返回查看(模块);
    }

我已经证明了它的价值包含在视图如下:

 < D​​T> ID< / DT>
    < D​​D> @ Model.Id< / DD>    < D​​T>模块ID< / DT>
    < D​​D> @ Model.ModuleId< / DD>     < D​​T>拥有者LT; / DT>
    < D​​D> @ Model.Owner< / DD>    < D​​T>模块类型< / DT>
    < D​​D> @ Model.TypeName< / DD>    < D​​T>模块类和LT; / DT>
    < D​​D> @ Model.KindName< / DD>    < D​​T>所有权开始日期和LT; / DT>
    < D​​D> @ Model.Start< / DD>    < D​​T>归属结束日期< / DT>
    < D​​D> @ Model.End< / DD>    @foreach(在Model.Properties VAR属性)
    {
        < D​​T>属性名称< / DT>
        < D​​D> @ properties.Name< / DD>
        < D​​T>属性值< / DT>
        < D​​D> @ properties.Value< / DD>
    }

目前 @ Model.End 为空,这是DateTime类型的,我已经将它设置为在视图模型为空。
既然是零,这就是我得到鉴于

正如你所看到的,所有权结束日期的值是从下面走属性名称的值。我怎样才能将其设置为空当的 @ Model.End为空的?

编辑1:

我的模型

 公共类OwnedModuleDetails
{
    众长标识{搞定;组; }    公共字符串的moduleId {搞定;组; }    公共字符串所有者获得{;组; }    公共字符串KindName {搞定;组; }
    公共字符串类型名{获得;组; }    公开日期时间启动{搞定;组; }    公众的DateTime?结束{搞定;组; }    公共IEnumerable的<性>属性{搞定;组; }
}

从资源库法

 公共OwnedModuleDetails GetModuleDetails(长ID,串所有者)
        {
// ReSharper的禁用ImplicitlyCapturedClosure
            VAR模块=(_dbSis.OwnedModules.Where(T => t.Id == ID)。选择(M =>新建OwnedModuleDetails
// ReSharper的恢复ImplicitlyCapturedClosure
            {
                ID = ID,
                的moduleId = m.ModuleId,
                类型名= m.ModuleType.TypeName,
                KindName = m.ModuleType.ModuleKind.KindName,
                所有者=所有者,
                启动= m.Start,
                结束= m.End,
                属性= m.PropertyConfiguration.PropertyInstances.Select(
                    X =>新属性{名称= x.Property.Name,值= x.Value})
            }));            返回(module.FirstOrDefault());
        }


解决方案

尝试添加一个空格:

 < D​​T>归属结束日期< / DT>
< D​​D>
    @if(Model.End!= NULL)
    {
        @ Model.End
    }
    其他
    {
        @:放大器; NBSP;
    }
< / DD>

From my controller I have passed value module to view

    public ActionResult Details(long id, string owner)
    {
        var module = _ownedModuleRepository.GetModuleDetails(id, owner);

        return View(module);
    }

I've shown the value it contains in view as follows

    <dt>ID</dt>
    <dd>@Model.Id</dd>

    <dt>Module ID</dt>
    <dd>@Model.ModuleId</dd>

     <dt>Owner</dt>
    <dd>@Model.Owner</dd>

    <dt>Module Type</dt>
    <dd>@Model.TypeName</dd>

    <dt>Module Kind</dt>
    <dd>@Model.KindName</dd>

    <dt>Ownership Start Date</dt>
    <dd>@Model.Start</dd>

    <dt>Ownership End Date</dt>
    <dd>@Model.End</dd>

    @foreach (var properties in Model.Properties)
    {
        <dt>Property Name</dt>
        <dd>@properties.Name</dd>
        <dt>Property Value</dt>
        <dd>@properties.Value</dd>
    }

Currently @Model.End is null, it is of DateTime type and I had set it to be nullable in viewmodel. Since it is null, this is what i'm getting in view

As you can see, the value of Ownership End Date is taking the value of Property Name from below. How can I set it to empty if the @Model.End is null?

Edit 1:

My model

public class OwnedModuleDetails
{
    public long Id { get; set; }

    public string ModuleId { get; set; }

    public string Owner { get; set; }

    public string KindName { get; set; }
    public string TypeName { get; set; }

    public DateTime Start { get; set; }

    public DateTime? End { get; set; }

    public IEnumerable<Property> Properties { get; set; }
}

Method from the repository

     public OwnedModuleDetails GetModuleDetails(long id, string owner)
        {
// ReSharper disable ImplicitlyCapturedClosure
            var module = (_dbSis.OwnedModules.Where(t => t.Id == id).Select(m => new OwnedModuleDetails
// ReSharper restore ImplicitlyCapturedClosure
            {
                Id = id,
                ModuleId = m.ModuleId,
                TypeName = m.ModuleType.TypeName,
                KindName = m.ModuleType.ModuleKind.KindName,
                Owner = owner,
                Start = m.Start,
                End = m.End,
                Properties = m.PropertyConfiguration.PropertyInstances.Select(
                    x => new Property { Name = x.Property.Name, Value = x.Value })
            }));

            return (module.FirstOrDefault());
        }

解决方案

Try adding a space:

<dt>Ownership End Date</dt>
<dd>
    @if (Model.End != null)
    {
        @Model.End
    }
    else
    {
        @:&nbsp;
    }
</dd>

这篇关于检查是否DateTime类型的值是针对空,如果空显示空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆