Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

asked on

asp.net mvc - Time

Hi Guys,

I have some weird issue. I'm querying Datetime field from the database and I'm trying to show it in the razor page.

I'm getting the value right in the controller, but when it comes to the view it shows - 01/00/1001.

Here is my code:

controller:
 public ActionResult Showzreport(int Batch)
 {
SonCRMEntities db = new SonCRMEntities();
            var z = (from i in db.ERPJournalza(Batch) select i).FirstOrDefault();

            Zreport zr = new Zreport();
            zr.Reportdate = z.ReportDate.Value;

return PartialView("_zreport", zr);
}

Open in new window


View:
@model Jurnalt.Models.Zreport
<br />

<div class="row">
    <div class="col-md-12">
        <img src="~/Img/Zreport.JPG" class="img-responsive" />
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-4">
        Report Date:
    </div>
    <div class="col-md-8">
        @string.Format("{0:MM/dd/yyyy}", Model.Reportdate)
    </div>
</div>

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

How is the DateTime field defined in Zreport class?
Avatar of Moti Mashiah

ASKER

Hi Fernando,

Public Datetime Reportdate {get; set;}
Hi Moti;

That looks right to me. Are you getting any errors or warnings?
No I don't get any errors.

I have to mention that when I opened a new project and did the same it works fine which actually confuse me the most :).

Also, it was working before and it stopped working after a while in my current project.

I was trying to figure if I have made any changes since it was working, but unfortunately I couldn't see any.
Sorry I have no idea why this is not working for you any why it stopped working. I will try and see if I can find anything that can solve your issue.
Thank you Fernando.

I'm still scratching my head here. Again I opened another project and copy the same code from my production and it works.

I don't know what is going on here. :).

This is what I'm getting in the view:

Report Date: 01/01/0001
Hi Moti;

Can you create a new small project that shows this issue then zip the complete project up and post it to a site where I can download it and try it on my computer?
Hi Fernando,

I can do it, but when I create new project everything works fine.

I think the issue is happening just in my project which is really weird :).

I have attached some troubleshooting I did. Please, can you look at it and tell me if you can figure what is wrong there.

User generated image
In your view you have this, @string.Format("{0:MM/dd/yyyy}", Model.Reportdate), but when you create the model you do this, Reportdates = a.ReportDate.Value, your view should be @string.Format("{0:MM/dd/yyyy}", Model.Reportdates)
Fernando,

I'm doing exactly what you suggested.

 @string.Format("{0:MM/dd/yyyy}", Model.Reportdates)
And what are the results of that?
Report Date: 01/01/0001
Fernando,

I would like to show you what I'm doing in my test project which its actually work.

Controller:
public ActionResult Testtime()
        {
            
            RMSmasterDBtestEntities db = new RMSmasterDBtestEntities();
            timemodel model = new timemodel();

            var z = (from i in db.ERPJournalza(50) select i).FirstOrDefault();

            model.Timedate = z.ReportDate.Value;

            return PartialView("_timedate",model);

        }

Open in new window


View
<div>
    @string.Format("{0:MM/dd/yyyy}", Model.Timedate)
</div>

Open in new window


public class timemodel
    {
        public DateTime Timedate { get; set; }
    }

Open in new window

I can't zip it and send it because I have built entity from the database and I can't send you all this huge database.

I believe you are getting the idea.
Can you post the complete class Zreport?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Jurnalt.Models
{
    public class Zreport
    {
        public Zreport()
        {
            Tenderlst = new List<Zreport>();
        }

        public DateTime Reportdates { get; set; }
        public TimeSpan Reporttime { get; set; }
        public int Register { get; set; }
        public int Batch { get; set; }
        public string Batchstatus { get; set; }
        public DateTime Startdate { get; set; }
        public TimeSpan Starttime { get; set; }
        public DateTime Date { get; set; }
        public TimeSpan Time { get; set; }

        public decimal Openingtotal { get; set; }
        public decimal Sales { get; set; }
        public decimal Return { get; set; }
        public decimal Tax { get; set; }
        public decimal Shipping { get; set; }
        public decimal Debitsurcharge { get; set; }
        public decimal Cashbackfee { get; set; }
        public decimal Paidonlayway { get; set; }
        public decimal Paidtoaccount { get; set; }
        public decimal Depositmade { get; set; }
        public decimal Voucher { get; set; }
        public decimal Total1 { get; set; }

        public decimal Paidout { get; set; }
        public decimal Dropped { get; set; }
        public decimal Layawayclosed { get; set; }
        public decimal Paidonaccount { get; set; }
        public decimal Depositreademe { get; set; }
        public decimal Closingtotal { get; set; }
        public decimal Total2 { get; set; }

        public decimal OverShort { get; set; }

        public decimal Totaltender { get; set; }
        public decimal Totalchange { get; set; }
        public decimal Discounts { get; set; }
        public decimal Costofgoods { get; set; }
        public decimal Commission { get; set; }
        public decimal Customercount { get; set; }
        public decimal Nosales { get; set; }
        public decimal Abortedtrans { get; set; }

        public decimal Taxcollected { get; set; }

        public List<Zreport> Tenderlst { get; set; }
        public string Description { get; set; }
        public decimal Amount { get; set; }
        public string Phase { get; set; }
        public decimal Layoutclosed { get; internal set; }
    }
}

Open in new window

Fernando.

I think I got what is going on here. Thanks to you :).

I will update you soon.

Thanks.
Hi Fernando,

I thought that I got the issue, but unfortunately no:).

My thought was to give my list items new class and then insert it to the constructor as I thought I'm reset the all properties when I send the same model.

Here is my class example:

namespace Jurnalt.Models
{
    public class Zreport
    {
        public Zreport()
        {
            Tenderlst = new List<Zreporttender>();
        }

        public DateTime Reportdates { get; set; }
        public TimeSpan Reporttime { get; set; }
        public int Register { get; set; }
        public int Batch { get; set; }
        public string Batchstatus { get; set; }
        public DateTime Startdate { get; set; }
        public TimeSpan Starttime { get; set; }
        public DateTime Date { get; set; }
        public TimeSpan Time { get; set; }

        public decimal Openingtotal { get; set; }
        public decimal Sales { get; set; }
        public decimal Return { get; set; }
        public decimal Tax { get; set; }
        public decimal Shipping { get; set; }
        public decimal Debitsurcharge { get; set; }
        public decimal Cashbackfee { get; set; }
        public decimal Paidonlayway { get; set; }
        public decimal Paidtoaccount { get; set; }
        public decimal Depositmade { get; set; }
        public decimal Voucher { get; set; }
        public decimal Total1 { get; set; }

        public decimal Paidout { get; set; }
        public decimal Dropped { get; set; }
        public decimal Layawayclosed { get; set; }
        public decimal Paidonaccount { get; set; }
        public decimal Depositreademe { get; set; }
        public decimal Closingtotal { get; set; }
        public decimal Total2 { get; set; }

        public decimal OverShort { get; set; }

        public decimal Totaltender { get; set; }
        public decimal Totalchange { get; set; }
        public decimal Discounts { get; set; }
        public decimal Costofgoods { get; set; }
        public decimal Commission { get; set; }
        public decimal Customercount { get; set; }
        public decimal Nosales { get; set; }
        public decimal Abortedtrans { get; set; }

        public decimal Taxcollected { get; set; }

        public List<Zreporttender> Tenderlst { get; set; }
       
        public decimal Layoutclosed { get; internal set; }
    }

    public class Zreporttender
    {
        public string Description { get; set; }
        public decimal Amount { get; set; }
        public string Phase { get; set; }
    }
}

Open in new window


Here is my controller
  public ActionResult Showzreport(int batch)
        {
            ALThompsonCRMEntities db = new ALThompsonCRMEntities();
        
            var zr = db.ERPJournalza(batch).Select(a => new
            Zreport
            {
                Reportdates = (DateTime)a.ReportDate,
                Reporttime = (TimeSpan)a.ReportTime,
                Register = a.RegisterNumber,
                Batch = a.BatchNumber,
                Batchstatus = a.Status,
                Startdate = (DateTime)a.StartDate,
                Starttime = (TimeSpan)a.StartTime,
                Date = (DateTime)a.StartDate,
                Time = (TimeSpan)a.ReportTime,
                Openingtotal = a.OpeningTotal,
                Sales = a.Sales,
                Return = a.Returns,
                Tax = a.Tax,
                Shipping = a.Shipping,
                Debitsurcharge = a.DebitSurcharge,
                Cashbackfee = a.CashBackFee,
                Paidonlayway = a.PaidonLayaway,
                Paidtoaccount = a.PaidToAccount,
                Depositmade = a.DepositMade,
                Voucher = a.Vouchers,
                Total1 = (decimal)a.Total_1,
                Paidout = a.PaidOut,
                Dropped = a.Dropped,
                Layoutclosed = a.LayawayClosed,
                Paidonaccount = a.PaidOnAccount,
                Depositreademe = a.DepositRedeemed,
                Closingtotal = a.ClosingTotal,
                Total2 = (decimal)a.Total_2,
                OverShort = (decimal)a.OverShort,
                Totaltender = a.TotalTendered,
                Totalchange = a.TotalChange,
                Discounts = a.Discounts,
                Costofgoods = a.CostOfGoods,
                Commission = a.Commission,
                Customercount = a.CustomerCount,
                Nosales = a.NoSalesCount,
                Abortedtrans = a.AbortedTransCount,
                Taxcollected = a.TaxCollected,
                Tenderlst = db.ERPJournalzb(batch).Select(b => new Zreporttender { Description = b.Description, Amount = (decimal)b.Amount, Phase = b.Phase }).ToList()
            }).FirstOrDefault();

            return PartialView("_zreport", zr);
        }

Open in new window


View:
@model Jurnalt.Models.Zreport
<br />

<div class="row">
    <div class="col-md-12">
        <img src="~/Img/Zreport.JPG" class="img-responsive" />
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-4">
        Report Date:
    </div>
    <div class="col-md-8">
        @string.Format("{0:MM/dd/yyyy}", Model.Reportdates)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Report Time:
    </div>
    <div class="col-md-8">
        @Model.Reporttime
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-4">
        Register:
    </div>
    <div class="col-md-8">
        @Model.Register
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Batch:
    </div>
    <div class="col-md-8">
        @Model.Batch
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Batch Status:
    </div>
    <div class="col-md-8">
        @Model.Batchstatus
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Start Date:
    </div>
    <div class="col-md-8">
        @Model.Startdate
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Start Time:
    </div>
    <div class="col-md-8">
        @Model.Starttime
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Date:
    </div>
    <div class="col-md-8">
        @string.Format("{0:MM/dd/yyyy}", Model.Date)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Time:
    </div>
    <div class="col-md-8">
        @Model.Time
    </div>
</div>

<br />
<div class="row">
    <div class="col-md-4">
        Opening Total:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Openingtotal)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Sales:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Sales)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Returns:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Return)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Tax:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Tax)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Shipping:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Shipping)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Debit Surcharge:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Debitsurcharge)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Cash Back Fee:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Cashbackfee)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Paid on Layaway:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Paidonlayway)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Paid to Account:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Paidonaccount)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Deposit Made:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Depositmade)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Vouchers:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Voucher)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Total:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Total1)
    </div>
</div>

<br />
<div class="row">
    <div class="col-md-4">
        Paid Out:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Paidout)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Dropped:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Dropped)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Layaway Close:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Layawayclosed)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Paid on Account:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Paidonaccount)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
       Deposit Redeeme:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Depositreademe)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Closing Total:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Closingtotal)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Total:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Total2)
    </div>
</div>

<br />
<div class="row">
    <div class="col-md-4">
        Over/Short:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.OverShort)
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-4">
        Total Tendered:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Totaltender)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Total Change:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Totalchange)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Discount:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Discounts)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Cost Of Goods:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Costofgoods)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Commission:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Commission)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Customer Count:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Customercount)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        No Sales:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Nosales)
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        Aborted Trans:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Abortedtrans)
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-4">
        Tax Collected:
    </div>
    <div class="col-md-8">
        @string.Format("{0:C}", Model.Taxcollected)
    </div>
</div>

<br />
@{ int i = 0;}
@foreach (var tn in Model.Tenderlst)
{
    <div class="row">
        <div class="col-md-4">
            @tn.Description:
        </div>
        <div class="col-md-8">
            @string.Format("{0:C}", tn.Amount) @tn.Phase
        </div>
    </div>
    i++;
    if (i == 9)
    {
        <br />
        i = 0;
    }

}

Open in new window

Hi Moti;

What do you get if you change this in your View
<div class="col-md-8">
    @string.Format ( "{0:MM/dd/yyyy}", Model.Reportdates )
</div>

Open in new window

To this
<div class="col-md-8">
    @Model.Reportdates.ToString( "MM/dd/yyyy" )
</div>

Open in new window

Same result.
I tested this on my computer and it worked. When I tested the way you have it I was getting an error. If you zip your project up and post it to a site where I can download it I will take a look at it.
I want to do it, but how can I zip this project ? as I have lots of entities which connected to the database.

I can send it as it is but you will not be able to run it without the database.
In creating your application do you not create a test db with  fictitious data ? If no then just post the project.
No I didn't do it as in this case it is project which works with lots of databases.

Anyway I will upload the project and let you know from where to pick it.

Thanks,
You can download it from this link.

https://drive.google.com/folderview?id=0B-7mE-wbnfBtcml2MnF4ckhlajg&usp=sharing

thank you Fernando.
HI Fernando,

I just want to speed things up and take you to the point when I have the issue in the code I sent you.

I have button which located in partial view _buttons
 <input type="button" class="btn btn-primary" id="zreportid" value="Z Report" data-toggle="modal" onclick="Openzreportdialog()" />

Open in new window



When I click on this button I get popup dialog which show me the report.

You can go through this javascript method "Openzreportdialog" and follow all my steps.

Thank you again.
You have some issues in your code, please read the comments below.
var zr = db.ERPJournalza(batch).Select(a => new
           Zreport
           {
              // Reportdates is defined as : public DateTime Reportdates { get; set; }
              // a.ReportDate is defined as : public Nullable<System.DateTime> ReportDate { get; set; }
              // What happens when a.ReportDate is null in the database? Even though you used a cast to DateTime 
              // it can't be converted. If you use a statement like this, Reportdates = a.ReportDate ?? XX where XX is assigned
              // to Reportdates but XX needs to be a valid date.
               Reportdates = (DateTime)a.ReportDate,
               Reporttime = (TimeSpan)a.ReportTime,

Open in new window

HI Fernando,

Yes, you are right. I defiantly would do something like that: Reportdates = a.ReportDate.Value != null ? a.ReportDate.Value : DateTime.Now, but how would I find solution to my issue.

Did you find anything?

Thank you soo much for help.
When you run the app and try many different values in the query does it always return DateTime.Min? Or does it only happens on  certain values?
Hi Fernando,

I had some chat with my SQL guy and he said that the stored procedure he created actually cast the date to "date" variable.

Now he changed it to "Datetime"  variable and I'm still getting the same result after I updated the entities, and yes I'm getting the Datetime.MIn return all the time.


Thank you sooo much for your help Fernando.
HI Fernando,

I have found the issue and now I don't know how to solve it.


This is my execution method:
        public ActionResult Showzreport(int batch)
        {
            ALThompsonCRMEntities db = new ALThompsonCRMEntities();

            var zrr = db.ERPJournalza(batch).Select(a => new
            Zreport
            {
                Testthisdate = string.Format("{0:MM/dd/yyyy}", a.ReportDate.Value),       
          
Phase = b.Phase }).ToList()
            }).FirstOrDefault();

            return PartialView("_zreporting", zrr);
        }

Open in new window


when I put execute interactive on the return it seems that it doesn't know about the partial method:
Look below:
Microsoft (R) Roslyn C# Compiler version 1.2.0.60325
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
>             return PartialView("_zreport", zrr);
. 
(1,20): error CS0103: The name 'PartialView' does not exist in the current context
(1,44): error CS0103: The name 'zrr' does not exist in the current context
>             return PartialView("_zreporting", zrr);
. 
(1,20): error CS0103: The name 'PartialView' does not exist in the current context
(1,47): error CS0103: The name 'zrr' does not exist in the current context
>             return PartialView("_zreporting", zrr);
. 
(1,20): error CS0103: The name 'PartialView' does not exist in the current context
(1,47): error CS0103: The name 'zrr' does not exist in the current context

Open in new window

I have many of other method in this controller which have no problem and get the partialview method with no problem.
Actually, I just figured that the message appears in all my partiaView, but at list in the other method I have everything working right.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, this is exactly what I do.

I have ajax that call the my method and popup the bootstrap dialog with this partial view:

look at this guy:
ajax call:
 function Openzreportdialog() {
            var Batch = $("#batch").val();
            $.ajax({
                type: "GET",
                url: "/Zreports/Showzreport",
                dataType: "html",
                data: {batch: Batch},
                success: function (data) {
                    $("#Zreportpopid").modal('show');
                    $("#zreportid").html(data);
                }
            });
        }

Open in new window



The popup dialog:
@*Zreport*@
<div id="Zreportpopid" class="modal modal-wide fade" aria-hidden="true" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header" style="background-color:lightblue">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4> <input type="button" class="btn btn-primary" value="Print" /> </h4>
            </div>
            <div class="modal-body">
                <div id="zreportid">
                   @Html.Partial("_zreporting", Model.Zreport )
                </div>
            </div>
        </div>
    </div>
</div>

Open in new window

Dear Fernando.

Thank you for your great help. finally I have found the issue and solve it.
I'm really embarrassing to tell you what happened :).

The only problem was duplicate element in the view side.
I was sending ajax to the controller and return partial to my div which contain id="zreportid" , but unfortunately I gave the same id for a button which located in the other partialview.

The controller return the right things, but then it would recognize the button element id and show nothing basically.

Thank you again.
Thank you man.