设计第一个Datetime密钥.我的脚手架控制器正在查找MM/dd/yyyy,而不是dd/MM/yyyy [英] Design First Datetime Key. My scaffolded controller is finding MM/dd/yyyy instead of dd/MM/yyyy

查看:81
本文介绍了设计第一个Datetime密钥.我的脚手架控制器正在查找MM/dd/yyyy,而不是dd/MM/yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供一些帮助:

设计优先(访问转换)"组合键是CalendarDate和Round.

Design First (Access Conversion) Composite Key is CalendarDate and Round.

来自Index.cshtml

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>

通过Edit.cshtml找到记录01/01/2018,1并可以对其进行更新.

A record of 01/01/2018,1 is found with Edit.cshtml and can be updated.

记录01/02/2018,1找到02/01/2018(如果记录存在).

A record of 01/02/2018,1 finds 02/01/2018 (if record exists).

记录02-02/2018,1找到01/02/2018(如果记录存在).

A record of 02/01/2018,1 finds 01/02/2018 (if record exists).

看起来路由使用的是美国日期格式,而不是AU格式.

Edit.cshtml的一部分

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

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

在CalendarsController.cs中获取

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

已尝试将以下内容添加到Startup.cs ******不变**.

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

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

已检查服务器区域设置:dd/MM/yyyy

Have checked Server region settings: dd/MM/yyyy

尝试将Route添加到Startup.cs-*****不变******

Have tried adding Route to Startup.cs - ***** No Change ******

app.UseMvc(routes =>

app.UseMvc(routes =>

        {
            routes.MapRoute(
                name: "Calendar",
                template: "{controller=Home}/{action=Index}/{CalendarDate?}/{Round?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        }

想法耗尽

推荐答案

感谢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 { 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> 

这可以解决路由问题,并且编辑"现在可以正常运行.

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

这篇关于设计第一个Datetime密钥.我的脚手架控制器正在查找MM/dd/yyyy,而不是dd/MM/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆