MVC 5 Date Format is displaying as MM/dd/yyyy in E

2019-09-21 09:46发布

问题:

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);
    }

回答1:

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.