I have a model including two different dates. Both these dates are set up similar to each other in the model.
[Required] [Display(Name = "Booking Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:D}")] public DateTime BookingDate { get; set; } .... [Display(Name = "Invoice Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:D}")] public DateTime? InvoiceDate { get; set; }
I have then created a view component, including these dates.
public IViewComponentResult Invoke(int id) { var jobs = from j in ctx.Job .Include(j => j.Site) .Include(j => j.WaterBody) .Where(j => j.Site.SiteID == id && j.InvoiceDate != null) .OrderByDescending(j => j.BookingDate) .Take(6) select j; return View(jobs); }
I assume the rest of the view component isn't relevant.
Then in the default view I have:
@foreach (var j in Model) {<tr><td> @j.BookingDate.ToString("dd MM yyyy")</td><td> @j.JobDescription.Substring(0, Math.Min(j.JobDescription.Length, 38))</td><td> @j.InvoiceDate</td><td><a asp-controller="Jobs" asp-action="Details" asp-route-id="@j.JobID">More Details</a></td></tr> }
All I want to do is format Invoice date the same as Booking Date. However every time I try it is throwing an error. It makes no sense. Why would one take the ToString Argument happily and the other refuse it? How can I get this date to play along? Why am I having to format the dates at all given they are formatted already in the Model?