Checking if a DateTime value is null in the view and shows a space if null

From my controller, I passed a value module to view

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

I showed the value that it contains, as shown below.

  <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 a DateTime type, and I set it to nullable in the viewmodel. Since this value is null, this is what I see

enter image description here

As you can see, the value of the end date of ownership takes the value of Property Name from the bottom. How can I set it empty if @ Model.End is null?

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

Repository Method

  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()); } 
+4
nullable asp.net-mvc view asp.net-mvc-4
Jul 29 '13 at 7:59 on
source share
4 answers

Try adding a space:

 <dt>Ownership End Date</dt> <dd> @if (Model.End != null) { @Model.End } else { @:&nbsp; } </dd> 
+5
Jul 29 '13 at 8:17
source share

I recently had this problem when a user inserted a column with a null value in a date field

which lead to this error:

System.InvalidOperationException: Nullable object must have a value

to solve this problem instead of DisplayFor , just use the code below in your view, so if the column value is null, it will display "Date is empty"

  <td> @(item.Startdate.HasValue ? item.Startdate.Value.ToString("dd/MM/yyyy") : "Date is Empty") </td> 

further reading of this and this for the model

Hope helps someone.

+3
Apr 20 '15 at 8:23
source share

The ternary operation will not work:

 @(Model.End != null ? Model.End.ToString() : "") 
0
Jul 29 '13 at 8:02
source share

You can use the C # null merge operator:

 @(Model.End?.ToString("dd/MM/yyyy") ?? "Date is Empty") 
0
Dec 19 '18 at 5:41
source share



All Articles