2011-03-21 4 views
1

다음과 유사한 내용인스턴스가있는 속성의 필수 속성

class EntityNameId 
{ 
    public string Name { get; set; } 
    public string Id { get; set; } 
} 

class OrganizationNameId : EntityNameId 
{ 
} 

class PersonViewModel 
{ 
    public PersonViewModel() 
    { 
     Organization = new OrganizationNameId(); 
    } 
    OrganizationNameId Organization { get; } 
} 

클라이언트 쪽 유효성 검사가 작동 할 수 있도록 OrganizationNameId.IdRequired 특성을 OrganizationNameId.Id에 대해 어떻게 설정할 수 있습니까? OrganizationNameId 또는 EntityNameId에 넣고 싶지는 않습니다. 왜냐하면 항상 필요하지는 않지만 PersonViewModel에는 필요하기 때문입니다. MVC 3을 사용하고 있습니다.

편집 : 아래에서 Bas에 설명 된대로 사용자 지정 속성을 추가하고 싶지 않습니다. EntityNameId을 재사용 할 수있는 사용자 정의 부분 뷰가 있습니다.

+0

확인이 이것을, 난 이미 여기이 질문에 http://stackoverflow.com/a/24757520/3050647 – elia07

답변

0

하나의 옵션은 조직 속성에 바인딩 PersonViewModel에 속성을 추가하는 것입니다?

+0

내가 '라는 이름의 사용자 정의 부분 뷰를 가지고이 일을하지 선호 대답 EntityNameId' . –

0
public class EntityNameId 
{ 
    public string Name { get; set; } 
    public string Id { get; set; } 
} 

[MetadataType(typeof(OrganizationNameIdMetadataType))] 
public class OrganizationNameId : EntityNameId 
{ 
} 

public class PersonViewModel 
{ 
    public PersonViewModel() 
    { 
     Organization = new OrganizationNameId(); 
    } 
    public OrganizationNameId Organization { get; private set; } 
} 

public class OrganizationNameIdMetadataType 
{ 
    [Required] 
    public object Id { get; set; } 
} 

Customizing ASP.NET MVC 2 - Metadata and Validation

어떻게 사용 MetadataType 속성에 대해 :

+0

나는 모든 경우에 그것이 요구되기를 원하지 않는다. –

+0

실망시켜 죄송합니다. – takepara

관련 문제