当我想要dd/MM/yyyy时,MVC 5日期格式在“编辑"中显示为MM/dd/yyyy [英] MVC 5 Date Format is displaying as MM/dd/yyyy in Edit when I want dd/MM/yyyy
问题描述
我知道这是一个老问题,已经回答了很多次,但是我搜索了几个月却没有解决我的问题. 我的应用程序仅在本地使用,并且需要启用.我是新手,我发现的答案与MVC 5没有的全局文件有关,因此非常困惑(我假设我使用Startup.cs?).不知道我是否需要自定义路线或自定义模型联编程序.
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.
型号
{
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; }
}
}
控制器
//GET:SuspensionHists/Edit/5 公共异步任务Edit(int?AssociId,short?SuspensionNo,DateTime SuspensionDate) { 如果(AssociId == null) { 返回NotFound(); }
// 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);
}
推荐答案
感谢Yas Ikeda的以下帮助:问题在于网址中的"/".在索引视图"上,我已更改以下链接:
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") })
到
<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.
这篇关于当我想要dd/MM/yyyy时,MVC 5日期格式在“编辑"中显示为MM/dd/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!