Avatar of Alon Wood
Alon Wood
Flag for United States of America asked on

How can I add calculated days duration between two dates on an Entity Frameworks Details.cshtml page?

I have not done much front end development in the past several years.  I just created a ASP.Net Core CRUD App With Entity Framework Razor Pages.  The separate Edit/Delete/Details pages were created automatically.  I am attempting to add several values to be shown on the details page.  These values will be integers of the number of days between two dates in the database table.  On the Details.cshtml page, I am trying to do something like:

<dt class="col-sm-2">
            Days to Email Officer
</dt>
<dd class="col-sm-10">
            <label>@(DateDiff('Days', model => model.EAB_AppealsMaster.FilingDate, model => model.EAB_AppealsMaster.EmailHODate))</label>
</dd>

Open in new window


I have much to learn about this environment and would appreciate any help on the simplest solution possible.  


My Details.cshtml page looks like this:


@page
@model EAB_CaseManager_EF.Pages.DetailsModel

@{
    ViewData["Title"] = " EAB Detail Listing";
}

<h1>Details</h1>

<div>
    <h4>EAB_AppealsMaster</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.DocketNo)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.DocketNo)
        </dd>

        <dt class="col-sm-2">
            Days to Email Officer
        </dt>
        <dd class="col-sm-10">
            <label>@(DateDiff('Days', model => model.EAB_AppealsMaster.FilingDate, model => model.EAB_AppealsMaster.EmailHODate))</label>
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.FilingDate)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.FilingDate)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.Appellant)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.Appellant)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.Attorney)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.Attorney)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.Agency)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.Agency)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.Action)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.Action)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.Officer)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.Officer)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.EmailHODate)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.EmailHODate)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.EAB_AppealsMaster.HearingDate)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.EAB_AppealsMaster.HearingDate)
        </dd>

Open in new window


My table definition, built from a SQL Server table looks like this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace EAB_CaseManager_EF.Models
{
    public class EAB_AppealsMaster
    {
    [Key]
    public int Docket_ID { get; set; }
    public string? DocketNo { get; set; }
 
    [DataType(DataType.Date)]
    public DateTime? FilingDate { get; set; }
   
    public string? Appellant { get; set; }
    public string? Attorney { get; set; }
    public string? Agency { get; set; }
    public string? Action { get; set; }
    public string? Officer { get; set; }

    [DataType(DataType.Date)]
    public DateTime? EmailHODate { get; set; }
 
    [DataType(DataType.Date)]
     public DateTime? HearingDate { get; set; }

    public string? HearingLocation { get; set; }

    [DataType(DataType.Date)]
    public DateTime? RecordCloseDate { get; set; }
    public string? OrderType { get; set; }

    [DataType(DataType.Date)]
    public DateTime? OrderDate { get; set; }

    [DataType(DataType.Date)]
    public DateTime? FBdReqDate { get; set; }

Open in new window



ASP.NETDatabases* Entity Framework Core

Avatar of undefined
Last Comment
louisfr

8/22/2022 - Mon
Alon Wood

ASKER
I can add this snippet of code in the Details.cshtml.cs file, and I do get the correct value when stepping into the program, but when I try to use the FileToHearingOfficerDays value in the Details.cshtml page, it says the variable does not exist in the current context.   
Alon Wood

ASKER
I appear to be getting closer to a solution, even if it is not following best coding practices.  I can place this block of code in the Details.cshtml in an attempt to calculate the days between dates.  But now I get an error stating: Cannot convert lambda expression to DateTime because it is not a delegate type.  If I could just get past that error, I should be able to display the FileToHearingOfficerDays variable on the page.  I have not found how to remove the error thus far:


ASKER CERTIFIED SOLUTION
louisfr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck