0

In my edit VIEW I have the following code:

<div class="form-group">
    @Html.LabelFor(model => model.CREATED, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.CREATED, new { htmlAttributes = new { @class = "form-control", @readonly = true } })
        @Html.ValidationMessageFor(model => model.CREATED, "", new { @class = "text-danger"})
    </div>
</div>

As you can see the Created textbox is read only and is greyed out.

Validation fires wrongly

In my MODEL I have the following:

public Nullable<System.DateTime> CREATED { get; set; }

The problem is when you put the cursor into the greyed out field, and then go into another field, the validation fires wrongly.

How do i get round this problem?

Community
  • 1
  • 1
PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94
  • If it cannot be edited, then why are you creating a form control for it? And the error occurs because `jquery.validate.js` validates dates based on the `MM/dd/yyyy` format and your culture appears to be `dd/MM/yyyy` –  Sep 24 '16 at 11:36
  • Since the name of the property indicates its the date the record was created it should not be in the view anyway - it should only be set in the controller immediately before you save the data model (and your view should be using a [view model](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) without a `CREATED` property. –  Sep 24 '16 at 11:38
  • I want to display when it was created. Would i face this issue if created this value within a `div` ? – PriceCheaperton Sep 24 '16 at 11:57
  • @PriceCheaperton Don't create a TextBox for it, just display it as Text. – sachin Sep 24 '16 at 12:01
  • If your editing a existing object, then just use `
    @Html.DisplayFor(m => m.CREATED)
    ` (and style that div to look like a readonly textbox if you want) and remove `ValidationMessageFor()` You can always include `@Html.HiddenFor(m => m.CREATED)` if you want it to be submitted (but that would not be necessary if you do this correctly by using a view model)
    –  Sep 24 '16 at 12:02
  • Can you post this as an answer so i can give you credit? – PriceCheaperton Sep 24 '16 at 12:06
  • Its late here - will add detailed explanation in the morning :) –  Sep 24 '16 at 12:07
  • no worries, its 13:08 here in the UK... have a good evening! – PriceCheaperton Sep 24 '16 at 12:09
  • What format you want in this field ? Do you want to add time with date ? – Sunil Kumar Sep 24 '16 at 14:41

1 Answers1

1

The reason for the client side validation error is that you have generated a textbox for the property and by default, jquery.validate.js validates dates based on a MM/dd/yyyy format, whereas your format is dd/MM/yyyy. You could override the $.validator as explained in the answer to your subsequent question.

However the real issue is that you should not be generating a form control for a 'Create Date' (or 'Modified Date') property. Those values should only be set in the controllers POST method immediately before you save the object.

In the view, if your editing an existing object, use

<div>@Html.DisplayFor(m => m.CREATED)</div>

to display the date, and you can format the div to look like your other textboxes if necessary. You should also delete the associated ValidationMessageFor() and LabelFor() (you no longer have an associated form control so a <label> element is not appropriate - you can just use (say) <span>@Html.DisplayNameFor(m => m.CREATED)</span>)

If you wanted to submit the value of CREATED, then you can also include a hidden input for the value (by default, hidden inputs are not validated)

@Html.HiddenFor(m => m.CREATED) // you can use a [DisplayAttribute] for formatting the value

However, because you editing data, you should be using a view model, not your data model. In the POST method, you get the original data model from the repository based on its ID, and update its properties based on the values from the view model (making the need for the hidden input redundant because you do not need to update that value). This pattern also protects you from malicious users who may alter the request and send back changed values for the dates.

Community
  • 1
  • 1