
本文介绍了设计第一个日期时间键.我的脚手架控制器找到 MM/dd/yyyy 而不是 dd/MM/yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
希望得到帮助:
设计优先(访问转换)复合键是 CalendarDate 和 Round.
来自 Index.cshtml
@foreach(模型中的变量项目){<tr><td>@Html.DisplayFor(modelItem => item.CalendarDate)</td><td>@Html.DisplayFor(modelItem => item.Round)</td><td>@Html.DisplayFor(modelItem => item.SundayComp)</td><td>@Html.ActionLink("Edit", "Edit", new { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round }) |@Html.ActionLink("Delete", "Delete", new { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round })</td></tr>}</tbody>
在 Edit.cshtml 中找到 01/01/2018,1 的记录,可以更新.
01/02/2018 的记录,1 会找到 02/01/2018(如果记录存在).
02/01/2018 的记录,1 会找到 01/02/2018(如果记录存在).
看起来路由正在使用美国日期格式而不是 AU 格式.
Edit.cshtml 的一部分
<div asp-validation-summary="ModelOnly" class="text-danger"></div><input type="hidden" asp-for="CalendarDate"/><input type="hidden" asp-for="Round"/><input type="hidden" asp-for="SsmaTimeStamp"/><div class="form-group"><label asp-for="CalendarDate" class="control-label"></label><input asp-for="CalendarDate" class="form-control" disabled/><span asp-validation-for="CalendarDate" class="text-danger"></span>
<div class="form-group"><label asp-for="Round" class="control-label"></label><input asp-for="Round" class="form-control" 禁用/><span asp-validation-for="Round" class="text-danger"></span>
<div class="form-group"><div class="checkbox"><标签><输入 asp-for="SundayComp"/>@Html.DisplayNameFor(model => model.SundayComp)
<div class="form-group"><input type="submit" value="Save" class="btn btn-default"/>
</表单>
模型日历.cs
公共部分类日历{公共日历()[钥匙][列(订单 = 0)][数据类型(数据类型.日期)][DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]公共日期时间日历日期 { 获取;放;}[列(订单 = 1)]公共短轮{得到;放;}public bool SundayComp { get;放;}公共字节[] SsmaTimeStamp { 获取;放;}}
CalendarsController.cs 中的获取
//GET: Calendars/Edit/5公共异步任务编辑(日期时间?日历日期,短?圆){如果(日历日期 == 空){返回 NotFound();}如果(轮==空){返回 NotFound();}var calendar = await _context.Calendar.FindAsync(CalendarDate,Round);如果(日历 == 空){返回 NotFound();}返回视图(日历);}
已尝试将以下内容添加到 Startup.cs ****** 无变化 ******.
public void ConfigureServices(IServiceCollection services){services.Configure(options =>{options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-AU");options.SupportedCultures = 新列表{ new CultureInfo("en-AU") };options.RequestCultureProviders.Clear();});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
和
public void Configure(IApplicationBuilder app, IHostingEnvironment env){var supportedCultures = new[] { new CultureInfo("en-AU") };app.UseRequestLocalization(新的RequestLocalizationOptions{DefaultRequestCulture = new RequestCulture("en-AU"),支持的文化 = 支持的文化,支持的UICultures = 支持的文化});
已检查服务器区域设置:dd/MM/yyyy
已尝试将路由添加到 Startup.cs - ***** 无变化 ******
app.UseMvc(routes =>
{路线.MapRoute(name: "日历",模板:{controller=Home}/{action=Index}/{CalendarDate?}/{Round?}");路线.MapRoute(名称:默认",模板:{controller=Home}/{action=Index}/{id?}");}
没有想法
解决方案
感谢 Yas Ikeda 提供的以下内容: 问题与 url 中的/"有关.在索引视图中,我更改了链接:
@Html.ActionLink("Edit", "Edit", new { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round })
到
<a asp-action="Edit" asp-route-CalendarDate="@item.CalendarDate.ToString("o")" asp-route-Round="@item.Round">Edit</a>
这修复了路由问题,现在编辑可以正常工作了.
Would like some help please:
Design First (Access Conversion) Composite Key is CalendarDate and Round.
From Index.cshtml
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CalendarDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Round)
</td>
<td>
@Html.DisplayFor(modelItem => item.SundayComp)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round }) |
@Html.ActionLink("Delete", "Delete", new { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round })
</td>
</tr>
}
</tbody>
A record of 01/01/2018,1 is found with Edit.cshtml and can be updated.
A record of 01/02/2018,1 finds 02/01/2018 (if record exists).
A record of 02/01/2018,1 finds 01/02/2018 (if record exists).
Looks like routing is using US date format instead of AU format.
Part of Edit.cshtml
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="CalendarDate" />
<input type="hidden" asp-for="Round" />
<input type="hidden" asp-for="SsmaTimeStamp" />
<div class="form-group">
<label asp-for="CalendarDate" class="control-label"></label>
<input asp-for="CalendarDate" class="form-control" disabled/>
<span asp-validation-for="CalendarDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Round" class="control-label"></label>
<input asp-for="Round" class="form-control" disabled />
<span asp-validation-for="Round" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input asp-for="SundayComp" /> @Html.DisplayNameFor(model => model.SundayComp)
</label>
</div>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</form>
Model Calendar.cs
public partial class Calendar
{
public Calendar()
[Key]
[Column(Order = 0)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime CalendarDate { get; set; }
[Column(Order = 1)]
public short Round { get; set; }
public bool SundayComp { get; set; }
public byte[] SsmaTimeStamp { get; set; }
}
The Get in CalendarsController.cs
// GET: Calendars/Edit/5
public async Task<IActionResult> Edit(DateTime? CalendarDate, short? Round)
{
if (CalendarDate == null)
{
return NotFound();
}
if (Round == null)
{
return NotFound();
}
var calendar = await _context.Calendar.FindAsync(CalendarDate,Round);
if (calendar == null)
{
return NotFound();
}
return View(calendar);
}
Have tried adding following to Startup.cs ****** No change ******.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-AU");
options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-AU") };
options.RequestCultureProviders.Clear();
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
and
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var supportedCultures = new[] { new CultureInfo("en-AU") };
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-AU"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
Have checked Server region settings: dd/MM/yyyy
Have tried adding Route to Startup.cs - ***** No Change ******
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Calendar",
template: "{controller=Home}/{action=Index}/{CalendarDate?}/{Round?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
}
Running out of ideas
解决方案
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 { CalendarDate = item.CalendarDate.ToString("dd/MM/yyyy"), Round = item.Round })
to
<a asp-action="Edit" asp-route-CalendarDate="@item.CalendarDate.ToString("o")" asp-route-Round="@item.Round">Edit</a>
This fixes the routing problem and the Edit now works ok.
这篇关于设计第一个日期时间键.我的脚手架控制器找到 MM/dd/yyyy 而不是 dd/MM/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!