ASP MVC 2 Data Annotations not working with VS2008/ASP 3.5?
I have added data annotations to a 'buddy' class as referenced in MS guidance. In particular, the [DisplayName 开发者_运维技巧("Name")]
does not seem to be affecting anything. My understanding is that the value assigned to the annotation should be referenced and used by the Html.LabelFor(m => m.Attribute)
helper to display the label for the field.
Am I wrong on this?
I noticed in the view data class there is a strong-type view called EntityName+EntityName_Validation. Does that pull in additional components needed?
I tried creating a view using one of those types and there is no scaffolding present in the resulting view. Perhaps that is a different topic altogether. The UIHint
doesn't seem to have any impact either.
As mentioned, this is ASP.NET 3.5 code, in VS2008. I am using Linq to SQL. Perhaps this disqualifies the full use of data annotations too.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Sample.Models {
[MetadataType (typeof (SampleRequest_Validation))]
public partial class SampleRequest {
public class SampleRequest_Validation {
[DisplayName ("Description of Project:")]
[Required (ErrorMessage = "Project description is required.")]
[StringLength (500, ErrorMessage = "Project description cannot exceed 500 characters.")]
[UIHint ("TextArea")]
string ProjectDescription {get; set;}
I suspect I am missing some reference somewhere...
Thanks!
Okay, there was something missing...
I didn't declare the class attibutes as public, so of course the data annotations failed.
The last lisne from the code snippet above should read:
public string ProjectDescription {get; set;}
The smallest things...
精彩评论