-1

I know this is an old problem that has been answered many times but I have searched for several months without solving my problem. My app is local only and needs to be en AU. I am a newbe and answers I have found relate to a global file which MVC 5 does not have so am thoroughly confused (I assume I use Startup.cs ??). Don't know whether I need a custom route or a custom model binder.

Model

{
    public partial class SuspensionHist
    {
        [Key]
        [Column(Order = 0)]
        public int AssociId { get; set; }
        [Key]
        [Column(Order = 1)]
        public short SuspensionNo { get; set; }
        [Key]
        [Column(Order = 2)]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime SuspensionDate { get; set; }
        public short? OffenceNo { get; set; }
        public bool? SuspensionServed { get; set; }
        public byte[] SsmaTimeStamp { get; set; }
    }
}

Controller

// GET: SuspensionHists/Edit/5 public async Task Edit(int? AssociId, short? SuspensionNo, DateTime SuspensionDate) { if (AssociId == null) { return NotFound(); }

        if (SuspensionNo == null)
        {
            return NotFound();
        }

        if (SuspensionDate == null)
        {
            return NotFound();
        }

        var suspensionHist = await _context.SuspensionHist.FindAsync(AssociId, SuspensionNo, SuspensionDate);
        if (suspensionHist == null)
        {
            return NotFound();
        }
        ViewBag.SuspensionNo = new SelectList(_context.Suspension, "SuspensionNo", "SuspensionNo");
        return View(suspensionHist);
    }

    // POST: SuspensionHists/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int? AssociId, short? SuspensionNo, DateTime SuspensionDate, [Bind("AssociId,SuspensionNo,SuspensionDate,OffenceNo,SuspensionServed,SsmaTimeStamp")] SuspensionHist suspensionHist)
    {
        if (AssociId != suspensionHist.AssociId)
        {
            return NotFound();
        }

        if (SuspensionNo != suspensionHist.SuspensionNo)
        {
            return NotFound();
        }

        if (SuspensionDate != suspensionHist.SuspensionDate)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(suspensionHist);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SuspensionHistExists(suspensionHist.AssociId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(suspensionHist);
    }
Rod
  • 31
  • 6
  • 1
    How are you generating the input in the view? What is the culture on your server? –  Oct 30 '18 at 00:38
  • The server is set to Auto (but I have tried dd/MM/yyyy - no change). The Index view displays correctly: – Rod Oct 31 '18 at 02:33
  • It is a scaffolded controller. The Index view points at the Edit as below. @Html.ActionLink("Edit", "Edit", new { AssociId = item.AssociId, SuspensionNo = item.SuspensionNo, SuspensionDate = item.SuspensionDate }) | @Html.ActionLink("Delete", "Delete", new { AssociId = item.AssociId, SuspensionNo = item.SuspensionNo, SuspensionDate = item.SuspensionDate }) } – Rod Oct 31 '18 at 02:40
  • Edit View displays in US format.ie 15/06/2018 displays as 06/15/2018 00:00:00 and gives a not valid error on save. – Rod Oct 31 '18 at 02:47
  • Part of Edit View
    – Rod Oct 31 '18 at 02:49
  • Create works (and displays the date correctly).Delete displays correctly but gives an error on submit.ArgumentNullException: Value cannot be null. Parameter name: entity. Think it cant find the record. – Rod Oct 31 '18 at 02:54
  • Also refer https://stackoverflow.com/questions/52416537/design-first-datetime-key-my-scaffolded-controller-is-finding-mm-dd-yyyy-instea Different table same problem. – Rod Nov 01 '18 at 02:25
  • There is a whole lot of stuff here that is confusing. First you have `IActionResult` and are using TagHelpers` which means this is core-mvc, not mvc-5 as you have tagged it. –  Nov 01 '18 at 02:41
  • Then you have `[DataType(DataType.Date)]` which generates the browsers HTML-5 datepicker which will display the date in the browsers culture and post back in ISO format. But for that to work correctky then you must use `DataFormatString = "{0:yyyy-MM-dd}" in your `[DisplayFormat]` attribute (refer [Specify Date format (dd/MM/yyyy) ](https://stackoverflow.com/questions/43820926/specify-date-format-in-mvc5-dd-mm-yyyy/43826746#43826746) –  Nov 01 '18 at 02:41
  • Thanks for the reply Stephen. Sorry about the tagging. Just didn't know any better. Am not trying to use data picker, just trying to display all dates in dd/MM/yyyy format.Tried DataFormatString = "{0:yyyy-MM-dd}" in Model. That changes the Index view date format from dd/MM/yyyy to yyyy-MM-dd but does not change the edit view at all. it still displays as MM/dd/yyyy 00:00:00. This is the bit that is confusing me. The closest reference I have found is https://www.codeproject.com/Tips/1078167/DateTime-Issue-In-MV but I don't know how to apply it to my project (which is core-mvc as you say). – Rod Nov 02 '18 at 23:42
  • I have other tables within this app with dates and they display and update ok. The difference seems to be that the date in this table (and a couple of others also giving me trouble) is part of the key. I believe this is a routing problem rather than a display problem but havent found an answer as yet. – Rod Nov 05 '18 at 05:58

1 Answers1

0

Thanks to Yas Ikeda for the following: The problem is to do with '/' in the urls. On the Index View I have changed the link from:

@Html.ActionLink("Edit", "Edit", new { AssociId = item.AssociId, SuspensionNo = item.SuspensionNo, CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy") })

to

 <a asp-action="Edit" asp-route-Associid="@item.AssociId" asp-route-SuspensionNo="@item.SuspensionNo" asp-route-SuspensionDate="@item.SuspensionDate.ToString("o")">Edit</a>

This fixes the routing problem and the Edit now works ok.

Rod
  • 31
  • 6