Datetime-local at @ Html.EditorFor

2

I have a model property that I'll save DateTime :

[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:yyyy-MM-ddThh:mm:ssZ}")]
public DateTime DataInicio { get; set; }

I want to convert to an html field of type ( type="datetime-local" )

<input type="datetime-local">

As I'm trying to do:

 @Html.EditorFor(model => model.DataInicio, "{0:yyyy-MM-ddThh:mm:ssZ}", new  {type = "datetime-local" })

I put this formatting, put the type, put the attribute in the model, tried with ApplyFormatInEditMode = true and false and nothing works, at most it gets type="datetime" and nda.

It's getting like this:

<input class="text-box single-line" id="DataInicio" name="DataInicio" type="datetime" value="">
    
asked by anonymous 19.07.2016 / 16:11

1 answer

5

I do not know what version of MVC you are using, so I advise you to try to use TextBoxFor() , which has support for older versions.

What you should do is put @ before type , like this:

@Html.TextBoxFor(model => model.DataInicio, new {@type = "datetime-local"}) 

If you have MVC5 and want to use EditorFor() , you should use htmlAttributes , like this:

@Html.EditorFor(model => model.DataInicio, new { htmlAttributes = new { @type = "datetime-local" } })

See a working example on dotNetFiddle .

    
19.07.2016 / 16:25