在视图中检查 DateTime 类型的值是否为空,如果为空则显示为空 [英] Checking if the value of type DateTime is null in view and showing blank if null
本文介绍了在视图中检查 DateTime 类型的值是否为空,如果为空则显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从我的控制器中,我已将值模块传递给视图
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>
}
当前 @Model.End 为空,它是 DateTime 类型,我已在视图模型中将其设置为可为空.由于它是空的,这就是我所看到的
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
如您所见,所有权结束日期的值是从下面的属性名称的值中提取的.如果 @Model.End 为空,如何将其设置为空?
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?
编辑 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
{
@:
}
</dd>
这篇关于在视图中检查 DateTime 类型的值是否为空,如果为空则显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文