Moti Mashiah
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:
View:
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);
}
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>
How is the DateTime field defined in Zreport class?
ASKER
Hi Fernando,
Public Datetime Reportdate {get; set;}
Public Datetime Reportdate {get; set;}
Hi Moti;
That looks right to me. Are you getting any errors or warnings?
That looks right to me. Are you getting any errors or warnings?
ASKER
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.
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.
ASKER
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
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?
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?
ASKER
In your view you have this, @string.Format("{0:MM/dd/y yyy}", Model.Reportdate), but when you create the model you do this, Reportdates = a.ReportDate.Value, your view should be @string.Format("{0:MM/dd/y yyy}", Model.Reportdates)
ASKER
Fernando,
I'm doing exactly what you suggested.
@string.Format("{0:MM/dd/y yyy}", Model.Reportdates)
I'm doing exactly what you suggested.
@string.Format("{0:MM/dd/y
And what are the results of that?
ASKER
Report Date: 01/01/0001
ASKER
Fernando,
I would like to show you what I'm doing in my test project which its actually work.
Controller:
View
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);
}
View
<div>
@string.Format("{0:MM/dd/yyyy}", Model.Timedate)
</div>
public class timemodel
{
public DateTime Timedate { get; set; }
}
ASKER
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.
I believe you are getting the idea.
Can you post the complete class Zreport?
ASKER
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; }
}
}
ASKER
Fernando.
I think I got what is going on here. Thanks to you :).
I will update you soon.
Thanks.
I think I got what is going on here. Thanks to you :).
I will update you soon.
Thanks.
ASKER
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:
Here is my controller
View:
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; }
}
}
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);
}
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;
}
}
Hi Moti;
What do you get if you change this in your View
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>
To this
<div class="col-md-8">
@Model.Reportdates.ToString( "MM/dd/yyyy" )
</div>
ASKER
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.
ASKER
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.
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.
ASKER
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,
Anyway I will upload the project and let you know from where to pick it.
Thanks,
ASKER
You can download it from this link.
https://drive.google.com/folderview?id=0B-7mE-wbnfBtcml2MnF4ckhlajg&usp=sharing
thank you Fernando.
https://drive.google.com/folderview?id=0B-7mE-wbnfBtcml2MnF4ckhlajg&usp=sharing
thank you Fernando.
ASKER
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
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.
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()" />
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,
ASKER
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.
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?
ASKER
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.
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.
ASKER
HI Fernando,
I have found the issue and now I don't know how to solve it.
This is my execution method:
when I put execute interactive on the return it seems that it doesn't know about the partial method:
Look below:
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);
}
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
ASKER
I have many of other method in this controller which have no problem and get the partialview method with no problem.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:
The popup dialog:
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);
}
});
}
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">×</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>
ASKER
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 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.
ASKER
Thank you man.