0

I have found hints that MVC 2 recognises the 'buddy class' type of property metadata, where data annotation attributes are applied to a 'buddy' metadata class, and the MetadataType on the actual entity class points to that buddy class, as below. However, as below, it seems the only attribute that makes any difference to the rendered UI is DisplayName. Why are the other attributes like DataType, Required, and ReadOnly not working? I.e. why can I enter text in a read only field? Why do I not get an error when a required field is empty? Why does the DataType attribute have no apparent effect? Why does EditorForModel not include validation messages?

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    public class CustomerMetadata
    {
        [ScaffoldColumn(false)]
        public object CustomerId { get; set; }

        [DisplayName("CustomerNo.")]
        [ReadOnly(true)]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Customer No. is required.")]
        public object CustomerNo { get; set; }
    }
}

I find behaviour the same whether I use an explicit LabelFor and TextBoxFor for each model property, or a single EditorForModel for the whole model.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • What other attributes have you tried & are expecting to change the UI? Try DataType and UIHint attributes – Clicktricity Oct 07 '10 at 14:19
  • Why is your CustomerNo `ReadOnly`? – MyNameIsJob Oct 07 '10 at 14:36
  • @MyNameIsJob, my CustomerNo would normally be autogenerated. I added Required just in the interests of testing these attributes. – ProfK Oct 07 '10 at 14:44
  • @ProfK, what are you asking? Which attributes effect the MVC HtmlHelper templates? – John Farrell Oct 07 '10 at 14:46
  • 1
    Can anyone explain the down vote please? – ProfK Oct 07 '10 at 15:30
  • @jfar, in asnwering your question, knowing which attributes will answer mine, of why are some attributes not working. – ProfK Oct 07 '10 at 15:33
  • @ProfK, I downvoted. What are you asking? Generally a good bug report contains. 1) What is happening? 2) What I expect to happen? and 3) How somebody can reproduce. Screenshots, detailed descriptions. In this case your view code, a screenshot of what is happening would be helpful. Right now your ending your question with "Am I missing something?", what does that even mean? Maybe your question should be "What DataAnnotations attributes effect the rendered view when using the ****For HtmlHelpers?". – John Farrell Oct 07 '10 at 16:47
  • 1
    Thanks @jfar, I will edit my post a bit to make it a more precise question. I don't mind a down vote, but I also like to be reminded what I'm doing wrong, as sometimes it escapes me at the time I post. – ProfK Oct 07 '10 at 17:05
  • @ProfK - This is still pretty bad... "Not working" could mean dozens of things. Do you have QA where you work? How to you fix a bug with "Customer Screen Not Working" as the description? – John Farrell Oct 07 '10 at 19:47
  • Are you attempting to submit the object and the resulting page not show the validation messages? You only get validation messages when there's an error **after** you submit your request. – Buildstarted Oct 08 '10 at 15:20

2 Answers2

2
  1. Required only affects validation.
  2. Readonly only affects binding.

The ErrorMessage string is only output when you use the ValidationFor() method.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • EditorForModel is supposed to include validation. – ProfK Oct 08 '10 at 06:33
  • By default EditorForModel includes validation messages just fine. Making them `object` though the template doesn't know quite what to do with them. Change your `CustomerNo` to a `string` and see if it works for you then. (Both metadata and customer object) – Buildstarted Oct 08 '10 at 15:19
0

Because I was including an EnableClientValidation() call in my view, I was expecting these attributes to cause client side, Javascript validation to be performed, and validation messages to be displayed.

It turns out that merely including EnableClientValidation() is not alone enough, and one also has to modify the master page (or view if you're not using a master page), to include the following scripts:

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

I'm not sure the jQuery is required for validation or not, but I included it as advised and things work properly now.

ProfK
  • 49,207
  • 121
  • 399
  • 775