Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Critical bug(?). Problem with Date from Model to View. Not using ISO international format. #4633

Closed
Bartmax opened this issue May 13, 2016 · 6 comments
Assignees
Milestone

Comments

@Bartmax
Copy link

Bartmax commented May 13, 2016

There's a problem with the input date, the datatype and the view.
Data does't get binded to the view correctly.

Even i'm using en-us culture mvc doesn't use the correct format. I'm pretty sure the ISO international format (yyyy-MM-dd) is accepted for every culture but mvc is not using that.

Startup.cs

var cultureInfo = new CultureInfo("en-US");

app.UseRequestLocalization(
    new RequestLocalizationOptions()
    {
        DefaultRequestCulture = new RequestCulture(cultureInfo),
        SupportedUICultures = new[] { cultureInfo },
        SupportedCultures = new[] { cultureInfo }
    });

view.cshtml:

<form asp-action="Edit" enctype="multipart/form-data">
    <div class="form-horizontal">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <input type="hidden" asp-for="Id" />
        <div class="form-group">
            <label asp-for="ReleaseDate" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="ReleaseDate" class="form-control" />
                <span asp-validation-for="ReleaseDate" class="text-danger"></span>
            </div>
        </div>

viewmodel.cs:

        [DataType(DataType.Date)]
        public DateTime? ReleaseDate { get; set; }

generated html:

<input class="form-control" type="date" id="ReleaseDate" name="ReleaseDate" value="6/2/2015" />

rendering:
image

Chrome warning:

The specified value "6/2/2015" does not conform to the required format, "yyyy-MM-dd".
@rynowak
Copy link
Member

rynowak commented May 13, 2016

/cc @dougbu

@Eilon Eilon added this to the 1.0.0 milestone May 14, 2016
@Eilon
Copy link
Member

Eilon commented May 14, 2016

@Eilon Eilon assigned javiercn and unassigned dougbu May 14, 2016
@javiercn
Copy link
Member

It seems that we don't handle nullable instances, I would say that its a bug

(typeof(DateTime) == modelExplorer.ModelType || typeof(DateTimeOffset) == modelExplorer.ModelType))

This is the value from the debugging session:
image

This is the point in code where we format the value:

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataDictionary.cs#L415

This is the value from the debugging session:
image

You can use asp-format to workaround the issue giving it {0:yyyy-MM-dd} as the value

@Bartmax
Copy link
Author

Bartmax commented May 16, 2016

Problem also appears when I decorate with [DataType(DataType.Date)] even on non-nullable DateTime type.

@dougbu
Copy link
Member

dougbu commented May 16, 2016

@Bartmax are you using RC1? @javiercn's fixes were done in RC2. For RC1, you need to change the default rendering. In Startup.ConfigureServices(IServiceCollection services):

services.AddViewOptions(
    options => options.HtmlHelperOptions.Html5DateRenderingMode = Html5DateRenderingMode.Rfc3339);

Note this will probably not help the DateTime? case.

@Bartmax
Copy link
Author

Bartmax commented May 16, 2016

I'm using RC2. I'm seeing different results from what it's supposed to experience. I will try again just to be sure.

javiercn added a commit that referenced this issue May 20, 2016
javiercn added a commit that referenced this issue May 20, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants