Problem DataAnnotation DataType for Date

0

I have a problem with the DataAnnotation DataType Date, in my view the date is displayed perfectly as I put it in the DataFormatString, but the moment I edit it, it appears as if I had not specified a date before or was not stored in the database, I leave samples.

As seen in the view:

How it looks when I give it Edit

My code is as follows:

[Display(Name = "Fecha de Ingreso")]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "0:yyyy/MM/dd}")]
[DataType(DataType.Date)]
public DateTime fecha_ingreso { get; set; }

Sight code:

    <td>
        @Html.DisplayFor(modelItem => item.fecha_ingreso)
    </td>
//Vista del Edit
<div class="form-group">
            @Html.LabelFor(model => model.fecha_ingreso, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.fecha_ingreso, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.fecha_ingreso, "", new { @class = "text-danger" })
            </div>
        </div>
    
asked by ByGroxD 03.04.2017 в 21:57
source

4 answers

0

Fix it,

Replace this:

[Display(Name = "Fecha de Ingreso")]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "0:yyyy/MM/dd}")]
[DataType(DataType.Date)]
public DateTime fecha_ingreso { get; set; }

Because of this:

[Display(Name = "Fecha de Ingreso")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime fecha_ingreso { get; set; }

Explanation:

In HTML5 or HTML the input of type Date in its value must have the following structure yyyy-MM-dd or Year-Month-Day, leave the ApplyFormatInEditMode in True because as it is a column whose data type is DateTime contains values such as time and therefore can generate conflict, so use the DataFormatString to format the date by eliminating the time the minute and the second and enable it in the Edit to recover the established format.

    
answered by 04.04.2017 / 16:14
source
1

Replaces:

[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "0:yyyy/MM/dd}")]

By:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "0:yyyy/MM/dd}")]

And if it still bothers you, in the view:

Replaces:

@Html.EditorFor(model => model.fecha_ingreso, new { htmlAttributes = new { @class = "form-control" } })

By

@Html.TextBoxFor(m => m.fecha_ingreso, "{0:yyyy/MM/dd}")

TextBoxFor : Provide as a text entry html element corresponding to the specified expression. In a simple word, it will always render as an input text box regardless of the type of data of the property that is being linked to the control.

EditorFor : This control performs HTML markup based on the property data type. For example: Suppose there is a Boolean property in the model. To show this property in the view as a check box, we can use CheckBoxFor or EditorFor. Both will generate the same marking.

What is the advantage of using EditorFor?

As we know, depending on the type of data the property generates the html markup. So suppose tomorrow if we change the type of property data in the model, there is no need to change anything in the view. The EditorFor control will change the html mark automatically.

In other words:

Html.TextboxFor will always render a textbox <input type="text" ...

    
answered by 03.04.2017 в 22:28
1

Try this way it worked perfect for me:

[DisplayFormat(ApplyFormatInEditMode = 
true, DataFormatString = "{0:yyyy-MM-dd}")]

And I did not have to change the EditorFor .

Greetings.

    
answered by 05.04.2018 в 23:06
0

friend, I recommend you use ajax toolkit.

you could use it like this:

     <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="true">
</cc1:ToolkitScriptManager>


    <asp:TextBox ID="txtFinicio" class="form-control" Width="104px" runat="server" required></asp:TextBox>  
           <cc1:CalendarExtender ID="CalendarExtender1" PopupButtonID="txtFinicio" runat="server" TargetControlID="txtFinicio" Format="yyyy/MM/dd"> </cc1:CalendarExtender> 

Before HTML put this code:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

here is the link where you can download ajax:

link

    
answered by 03.04.2017 в 22:24