GibboK
GibboK

Reputation: 74008

MVC how to add MetaData to a class

I use MVC 3 and EF 4.3.1.

I have my class created with POCO and I'm using Metadata to add DataAnnotations on my properties.

I'm generating Controls and Views using Scaffolding EF.

My problems:

What I'm doing wrong here?

Thanks for your help on this

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;

    namespace MyProject.Models
    {
        [MetadataType(typeof(ReOptionMetaData))]
        public partial class ReOption
        {
            private class ReOptionMetaData
            {
                [Key]
                public int OptionId { get; set; }
                [Required]
                [StringLength(64)]
                public string Name { get; set; }
                public string Value { get; set; }
                [Required]
                [StringLength(256)]
                public string Description { get; set; }
                [StringLength(256)]
                public string NoteInternal { get; set; }    
            }
        }
    }

Upvotes: 0

Views: 356

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039588

Testing the website the OptionId (is a PK IDENTITY) appear as a TextField. I need hide this propertyand make sure the value is created automatically.

In the metadata you could use the HiuddenInput attribute which will generate a hidden input instead of a text field:

[Key]
[HiddenInput(DisplayValue = false)]
public int OptionId { get; set; }

[StringLength(256)] does not work and in the View there is not ClientValidation

Make sure you have included jquery.validate.js and jquery.validate.unobtrusive.js scripts to your page.

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

and that you have enabled unobtrusive client script validation in your web.config:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    ...
</appSettings>

Upvotes: 2

Pieter Germishuys
Pieter Germishuys

Reputation: 4886

You can use the ScaffoldColumnAttribute to not scaffold the OptionId column.

Upvotes: 1

Related Questions