加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 检查DateTime类型的值在视图中是否为null,如果为

发布时间:2020-12-16 07:19:25 所属栏目:asp.Net 来源:网络整理
导读:从我的控制器我已经通过值模块来查看 public ActionResult Details(long id,string owner) { var module = _ownedModuleRepository.GetModuleDetails(id,owner); return View(module); } 我已经在视图中显示了它包含的值如下 dtID/dt dd@Model.Id/dd dtModul
从我的控制器我已经通过值模块来查看

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

        return View(module);
    }

我已经在视图中显示了它包含的值如下

<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>
    }

目前@ Model.End为null,它是DateTime类型,我在viewmodel中将其设置为可为空.
因为它是null,所以这就是我所看到的

如您所见,所有权结束日期的值是从下面获取属性名称的值.如果@ Model.End为null,如何将其设置为空?

编辑1:

我的模特

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; }
}

来自存储库的方法

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());
        }

解决方法

尝试添加空格:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读