Design First Datetime Key. My scaffolded controlle

2019-03-02 07:19发布

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

1条回答
beautiful°
2楼-- · 2019-03-02 07:45

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.

查看更多
登录 后发表回答