Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
        }
like image 580
Cybercop Avatar asked Jul 29 '13 07:07

Cybercop


People also ask

How do you check if a DateTime variable is NULL?

Use model. myDate. HasValue. It will return true if date is not null otherwise false.

How can I check if a DateTime is NULL in SQL?

This way, in an aggregate, you should be able to test against a Date field to check if it is NULL. EDIT: Use the literal #1900-01-01# or NullDate() for Date fields, and the literal #1900-01-01 00:00:00# for DateTime fields. NullDate() will work for Date fields, but not for DateTime fields.

How do you handle NULL in DateTime?

Using the DateTime nullable type, you can assign the null literal to the DateTime type. A nullable DateTime is specified using the following question mark syntax. DateTime? The following is the code to implement Nullable Datetime.

Can a DateTime be NULL?

DateTime is by default not a nullable type. So it can never store null value.


2 Answers

Try adding a space:

<dt>Ownership End Date</dt>
<dd>
    @if (Model.End != null)
    {
        @Model.End
    }
    else
    {
        @:&nbsp;
    }
</dd>
like image 148
Darin Dimitrov Avatar answered Oct 24 '22 08:10

Darin Dimitrov


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.

like image 37
Shaiju T Avatar answered Oct 24 '22 07:10

Shaiju T