Date-place on @Html.Editorfor

Asked

Viewed 925 times

2

I have a model property that I will keep DateTime:

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

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

<input type="datetime-local">

Like I’m trying to do:

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

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

he’s getting like this:

<input class="text-box single-line" id="DataInicio" name="DataInicio" type="datetime" value="">
  • Try to take out Datetypeattribute and switch to @Html.Textboxfor(model => model.Datastart, "{0:yyyy-MM-ddThh:mm:ssZ}", new {type = "datetime-local" })

1 answer

5


I don’t know which version of MVC you are using, so I advise you to try using TextBoxFor(), which has support for older versions.

What must be done is to put the @ before the type, in this way:

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

If you are with the MVC5 and want to use the EditorFor(), you must use the htmlAttributes, in this way:

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

See a functional example on dotNetFiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.