2009-09-03 3 views
2

강력한 형식의보기 실패, 이메일 하나 이상의 수신자 내가 두 테이블, 이메일 및 수신자의 간단한 데이터 모델이

로 보낼 수 있습니다 내가 설정 데이터베이스에이 Linq to SQL 저장소를 만든 두 테이블은 컨트롤러와 강력한 형식의 뷰를 만들었습니다. 나는 데이터베이스에서 모든 레코드를 선택하고자 할 때

이 잘 작동

public IList<AllMailDetail> ListAll() 
    { 
     var allMail = 
      from m in _datacontext.mail_receiveds 
     join r in _datacontext.mail_recipients on m.DeliveryId equals r.DeliveryId 
      select new AllMailDetail { 
             DeliveryId = m.DeliveryId, 
             MessageId = m.MessageId, 
             SentFrom = m.SentFrom, 
             FilePath = m.FilePath, 
             FileName = m.FileName, 
             SentDateTime = m.SentDateTime, 
             ReceivedDateTime = m.ReceivedDateTime, 
             Subject = m.Subject, 
             SpamScore = m.SpamScore, 
             IsSpam = m.IsSpam, 
             SenderIP = m.SenderIP, 
             Header = m.Header, 
             SentTo = r.SentTo 
             }; 

     return allMail.ToList <AllMailDetail>(); 
    } 

컨트롤러는 단순히 강력한 형식의보기

에 저장소에서 콘텐츠를 전송 클래스

public class AllMailDetail 
{ 
    public int DeliveryId { get; set; } 
    public int? MessageId { get; set; } 
    public string SentFrom { get; set; } 
    public string FilePath { get; set; } 
    public string FileName { get; set; } 
    public string SentDateTime { get; set; } 
    public DateTime ReceivedDateTime { get; set; } 
    public string Subject { get; set; } 
    public byte? SpamScore { get; set; } 
    public bool? IsSpam { get; set; } 
    public string SenderIP { get; set; } 
    public string Header { get; set; } 
    public string SentTo { get; set; } 
} 

사용자 정의 유형

public ActionResult Index() 
    { 
     return View(_repository.ListAll()); 
    } 

데이터베이스에서 메일 레코드를 하나만 받으려면 deliveryId

public IQueryable<AllMailDetail> GetMail(int? id) 
    { 
     var allMail = 
      from m in _datacontext.mail_receiveds 
      join r in _datacontext.mail_recipients 
      on m.DeliveryId equals r.DeliveryId 
      where m.DeliveryId == id 
      select new AllMailDetail 
      { 
       DeliveryId = m.DeliveryId, 
       MessageId = m.MessageId, 
       SentFrom = m.SentFrom, 
       FilePath = m.FilePath, 
       FileName = m.FileName, 
       SentDateTime = m.SentDateTime, 
       ReceivedDateTime = m.ReceivedDateTime, 
       Subject = m.Subject, 
       SpamScore = m.SpamScore, 
       IsSpam = m.IsSpam, 
       SenderIP = m.SenderIP, 
       Header = m.Header, 
       SentTo = r.SentTo 
      }; 

     return allMail; 
    } 

와 컨트롤러 코드를 받아 다음 코드

public ActionResult Details(int? id) 
    {  
     var mail = _repository.GetMail(id); 

     if (mail == null) 
      return View("NotFound"); 

     return View(mail); 
    } 

나는 또한 상속 = "시스템을 갖는 강력한 형식의보기를 사용하여 하나의 레코드에 대한 출력을 표시하려고했다. 영문 페이지의 상단에 있지만 Web.Mvc.ViewPage 나는 사전에 전달 모델 항목이 'System.Data.Linq.DataQuery`1 [projectMail.Models.AllMailDetail 유형이다

다음과 같은 오류가 발생했습니다 ] '부 이 사전에는 projectMail.Models.AllMailDetail 유형의 모델 항목이 필요합니다.

나는 많이 검색 한 후이 오류를 고정 이 MVC LINQ to SQL Table Join Record Display

그래서 내보기가 더 이상 강력하게 형식화되지 않고 다음과 같이 내가 페이지를 구축

<% foreach (projectMail.Models.AllMailDetail item in (IEnumerable)ViewData.Model) 
    { %> 

     ...items... 

<% } %> 

이 잘 작동 가장 도움이 게시물을 발견 ,하지만 먼 길을가는 것처럼 보입니다. 내가 알아낼 수 없습니다 것은

  1. 왜 두 번째 쿼리 뷰가 강력하게이 작업을 할 수있는 방법
  2. 를 입력 할 때
  3. 왜 작동하지 않았다 된 IQueryable 할 필요가 않는 것입니다 강력한 형식의보기
  4. 이는

답변

3

흠을 SQL로 LINQ를 사용하여 MVC에 조인을 다루는 가장 좋은 방법은, 컨트롤러에 시도

return View(_repository.GetMail(id).SingleOrDefault()); 

IQueryable 데이터 소스를 AllMailDetail View에 바인딩하려고하면 위의 사항이 해결됩니다.

+0

그래, 그 질문의 다른 부분에 어떤 아이디어가 효과가 있었습니까? – best

+0

두 번째 쿼리는 해당 함수가 데이터를 반환 한 후에 더 많은 LINQ를 수행 할 수 있도록하려는 경우에만 IQueryable 일 필요가 없습니다. 조인을하는 방식은 훌륭합니다. – TWith2Sugars

관련 문제