You must set the property to nullable, and put the date-Annotation
Required
over the field.
As there is already another answer saying this, I will explain it in the most detailed way I can.
Nullable? That doesn’t make sense...
That makes sense yes... a view-model
is a model that serves to exchange data between the controller and the view.
If the view can appear with the value of the empty property when invoking the operation
CRUD of creation, so it makes sense that this property is nullable,
because this is the initial state, despite being invalid...
fact this which is treated by Annotation Required
hassle-free.
If you do not make the property nullable, then you cannot pass a
view-model for the creation view (ie, is the same as pass null
for the view)...
to my mind seems acceptable, but I like to have control,
so I always call the creation view like this:
return this.View(new MeuViewModel());
So when I need to pass some default value, to the creation view,
I can do this:
// eu quero que a opção 'OpcaoDoUsuario' já venha checked, na view de criação
return this.View(new MeuViewModel { OpcaoDoUsuario = true });
Or else I could change the builder of MeuViewModel
, already Initiating the default value.
Annotation Required
Required
serves to say: if the value associated with the property is null, or
empty, then the model is invalid. It doesn’t matter if the value of the property is
cancellable or not. It turns out that validation can be done on the client, or on
server. MVC 5 can use the attribute required
to perform client-side validation
which is not a rule. MVC 3 also has client validation. In all
previous cases, whether nullable property or not, none conflict
conceptual with the attribute Required
.
Use Html.Editorfor
In view use EditorFor
. There are several reasons for this:
this method will generate HTML fields with template prefixes correctly,
what is needed when working with master-detail and with Editortemplates.
this method will obey the formats indicated via Annotation:
DisplayFormat
, DataType
, among others.
this method will render the most appropriate controls to handle the
data type. If you don’t like the way it was rendered, you can easily
create an Editortemplate to get around the problem by using Annotation
UIHint("NomeDoEditorPersonalizado")
.
Do not use Html.Textboxfor
I can confirm that in both ASP.NET MVC 3 and 4 and 5,
TextBoxFor
reads and renders the value of the property passed to it,
unless the view-model passed is null.
If the property is a DateTime
, which is a value-type whose value
standard is 01/01/0001 00:00:00
then that is what will be rendered. If it is a
int
, the default value is 0
, and in that case that is what will be rendered.
Only the default value of the type will not be rendered if a value is set
manually.
Also, this method ignores all attempts to format it using
Annotations: DisplayFormat
, DataType
, UIHint
...
When to use Textboxfor
I see no reason to use this method. Never!
If you are going to use a date-Picker plugin, it requires a <input type="text" />
,
then use an Editortemplate, which moreover encapsulates the use of the plugin.
Do not use Html.Textbox
Do not use the method TextBox
to create a template field.
This method does not take into account the use of template prefix,
which is required in master-detail type views,
and in the use of editing templates EditorTemplates
where applicable
(ViewData.TemplateInfo.HtmlFieldPrefix
).
Reference
Paste the View, Actionresult, and the Class that generates such a View please ?
– user6026
Class property: [Datatype(Datatype.Date)] public System.Datetime Datetime Datetime Datedaddee { get; set; } View property <p class="line"> <label class="W100" for="> Output request date:</label> @Html.Editorfor(model => model.>
– user4849
In all browsers gives it?
– Matheus Bessa
@Html.Textboxfor(model => model. ???
– user6026
Yes, this is entered by the code itself
– user4849