2010-05-01 8 views
0

내 html로 액션 링크는 내가 예를 들어 .. 를 세부 사항을 볼 수있는보기로 나를 취 ... http://localhost:1985/Materials/Details/4 를하지만 오류를 보여줍니다이 모델 오류는 asp.net mvc에서 무엇이라고 말합니까?

The model item passed into the dictionary is of type 
'System.Data.Linq.DataQuery`1[CrMVC.Models.ConstructionRepository+Materials]' 
but this dictionary requires a model item of type 
'CrMVC.Models.ConstructionRepository+Materials'. 

그리고 내 모델이다

  public IQueryable<Materials> GetMaterial(int id) 
     { 
      return from m in db.Materials 
          join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id 
          where m.Mat_id == id 
          select new Materials() 
          { 
           Id = Convert.ToInt64(m.Mat_id), 
           Mat_Name = m.Mat_Name, 
           Mes_Name = Mt.Name, 
          }; 
     } 
     public class Materials 
     { 
      private Int64 id; 
      public string mat_Name; 
      public string mes_Name; 
      public Int64 Id 
      { 
       get 
       { 
        return id; 
       } 
       set 
       { 
        id = value; 
       } 
      } 
      public string Mat_Name 
      { 
       get 
       { 
        return mat_Name; 
       } 
       set 
       { 
        mat_Name = value; 
       } 
      } 
      public string Mes_Name 
      { 
       get 
       { 
        return mes_Name; 
       } 
       set 
       { 
        mes_Name = value; 
       } 
      } 
     } 
    } 

및 제 컨트롤러 방법 ...

public ActionResult Details(int id) 
{ 
    var material = consRepository.GetMaterial(id).AsQueryable(); 
    return View("Details", material); 
} 

어떤 제안이 있습니까?

+0

.net 2 (또는 그 이상)로 작업하고 계십니까? 그렇지 않으면 자동 속성을 사용하여 코드 양을 줄이고 더 읽기 쉽게 만들 수 있습니다. 예를 들어 다음을 참조하십시오. http://weblogs.asp.net/dwahlin/archive/2007/12/04/c-3-0-features-automatic-properties.aspx – M4N

답변

1

GetMaterial(id) 메서드는보기에 단 하나의 인스턴스 만 필요하므로 아마 하나의 Material 인스턴스 만 반환해야합니다. 예 :

public Materials GetMaterial(int id) 
{ 
    return (from m in db.Materials 
      join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id 
      where m.Mat_id == id 
      select new Materials() 
      { 
       Id = Convert.ToInt64(m.Mat_id), 
       Mat_Name = m.Mat_Name, 
       Mes_Name = Mt.Name, 
      }).FirstOrDefault(); 
    } 
+0

@Martin이 방금 변경되었습니다. 반환 유형 ... It 근무했습니다. –

+0

@Martin 내 저장소 클래스 안에 재료 클래스가 있습니다 .. 좋은 연습인가요 아니면 그것을 밖으로 이동해야합니다 ... 어떤 제안 .. –

+0

@ Pandiya : 나는 도메인 모델을 분리 할 것이다. Materials 클래스)를 리포지토리에서 가져옵니다. – M4N