Checking if the value of type DateTime is null in view and showing blank if null
Asked Answered
N

5

5

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

enter image description here

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());
        }
Nigercongo answered 29/7, 2013 at 7:59 Comment(4)
It's weird how you could get a string value inside a DateTime? type. Are you sure that the End property is DateTime? ? Where is this MicrophoneArrayRackModuleId coming from?Decurved
It is set to date and time, I have put my Model as an edit, also the method controllers calls in repositoryNigercongo
Where is this MicrophoneArrayRackModuleId coming from? You cannot possibly get such output for a DateTime property.Decurved
My model has IEnumerable<Property> Properties Property class contains Name (string) and Value (string), MicrophoneRackModuleId is one of the Proerties NameNigercongo
D
8

Try adding a space:

<dt>Ownership End Date</dt>
<dd>
    @if (Model.End != null)
    {
        @Model.End
    }
    else
    {
        @:&nbsp;
    }
</dd>
Decurved answered 29/7, 2013 at 8:17 Comment(4)
For first option I get error There is no implicit conversion between 'string' and 'system.web.Ihtmlstring' for second option right in : of @:&nbsp; it says @ must be followed by valid code block. I tried removing @: but in browser it shows Ownership End Date as if (Model.End != null) { } else { }Nigercongo
The if statement should be prefixed with @.Decurved
yea, I forgot. Thanks, it worked. So if you have a null value then value from next item comes to the previous item? Like in my case.Nigercongo
No, it's a CSS issue with your <dt> and <dd> tags. If it is empty apparently the browser displays the next one. You should try fixing your CSS file.Decurved
L
5

I had this issue once, when user inserted null value in date column.

Which lead to this error:

System.InvalidOperationException: Nullable object must have a value

To solve this instead of DisplayFor just use below code 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 this and this for model

Hope helps someone.

Lakesha answered 20/4, 2015 at 8:23 Comment(0)
B
1

you can use C# null coalescing operator:

@(Model.End?.ToString("dd/MM/yyyy") ?? "Date is Empty")
Bridwell answered 19/12, 2018 at 5:41 Comment(0)
S
0

Won't a ternary operation work here:

@(Model.End != null ? Model.End.ToString() : "")
Starshaped answered 29/7, 2013 at 8:2 Comment(0)
A
0
@model.date.toString() == "01/01/0001"

After converting datetime into string you're now able to compare DateTime value.

Atingle answered 24/3, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.