2013-05-20 2 views
0

getFirstStudent 메서드가있는 인터페이스를 구현하려고합니다. 내 생각은 부분 뷰를 사용하는 모든 뷰 모델에서이 메서드를 사용할 수있게 만드는 것입니다. 이 방법으로, 여러 뷰 모델을 (이론적으로는 불가능한) 부분 뷰로 전달하는 문제를 해결할 것입니다.ASP.NET mvc4의 인터페이스 작업

인터페이스

public Interface IMiniView 
{ 
     string GetFirstStudentId(); 
} 

FirstViewmodel :

namespace XXX.ViewModel 
{ 
    public class StudentsViewModel: IMiniView 
    { 
     public IEnumerable<StudentInfoViewModel> StudentList { get; set; } 

     public string GetFirstStudentId() 
     { 
      return = ??????? 
     } 
    } 
} 

내가 아니라 내 부분보기의 첫 번째 학생을 얻기 위해 사용 ... 내가 접근하고 싶습니다

이제
@Model.StudentsViewModel.StudentList.ElementAt(0).StudentID 

GetFirstStudent 메서드에서. StudentInfoViewModel에는 StudentID라는 문자열이 있습니다.

답변

0

이 정보가 도움이됩니까?

namespace CivicaEducation.Business.Ces.ViewModel 
{ 
    public class StudentsViewModel: IMiniView 
    { 
     public IEnumerable<StudentInfoViewModel> StudentList { get; set; } 

     private StudentInfoViewModel FirstStudent 
     { 
      get 
      { 
       if (StudentList == null || StudentList.Count() == 0) { 
        return null; 
       } 

       return StudentList.FirstOrDefault(); 
      } 
     } 

     public string GetFirstStudentId() 
     { 
      return FirstStudent == null : string.Empty ? FirstStudent.Id; 
     } 

        // you can add more methods/properties to you interface like this 
     public string GetFirstStudentName() 
     { 
      return FirstStudent == null : string.Empty ? FirstStudent.Name; 
     } 
    } 
} 
+0

회신 안녕 케인 덕분에 ... 그러나 컴파일러는 doesnot FirstorDefault에 대한 모든 defition을 포함하거나 – user1032957

+1

는'System.Linq' 네임 스페이스에 대한 참조를 추가 카운트 뷰 모델 말을 좋아하지 않는다 – Kane

0

이 값을 사용할 수 있습니다.

return StudentList.First().SutdentId; 
관련 문제