하루의 좋은 시간! 모델을 보려면이 쿼리를 전달하는 방법을법인으로 Linq | 모델에 테이블 조인 통과
var getPhotos = (from m in db.photos
join n in db.comments on m.id equals n.photoid
where n.ownerName == User.Identity.Name
orderby n.id descending
select new {
m.imgcrop, m.id,
n.commenterName, n.comment
}).Take(10);
, 그리고 모델이 볼 수 :
나는 컨트롤러에서 쿼리와 MVC 프로젝트가 있습니다.저녁을 보내서 예제를 찾으십시오. 도와 주셔서 감사합니다! 전체 모델 클래스
업데이트
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace photostorage.Models { public class GlobalModel { public class PhotoViewModel { public photos Photos { get; set; } public profiles Profile { get; set; } public IQueryable<comments> Comments { get; set; } public IQueryable<photos> NextPrev { get; set; } } public class UserPhotoList { public IQueryable<photos> Photos { get; set; } public profiles Profile { get; set; } } public class UserProfileView { public IQueryable<photos> Photos { get; set; } public profiles Profile { get; set; } } public class GetLastComments { public IQueryable<photos> uPhoto { get; set; } public IQueryable<comments> uComments { get; set; } } } }
컨트롤러 :
당신이 당신의보기가 아니라에서만 사용됩니다 "보기 모델"을 만들 수 있습니다이 경우using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;
namespace photostorage.Controllers
{
public class HomeController : Controller
{
private photostorageEntities db = new photostorageEntities();
public ActionResult Index()
{
if(Request.IsAuthenticated) {
GlobalModel.GetLastComments model = new GlobalModel.GetLastComments();
var getPhotos = (from m in db.photos
join n in db.comments on m.id equals n.photoid
where n.ownerName == User.Identity.Name
select new {
m.imgcrop, m.id,
n.commenterName, n.comment
}).Take(10);
return View("Index_Auth", model);
}else{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View("Index");
}
}
public ActionResult About()
{
return View();
}
}
}
귀하의보기 모델이 어떻게 보이는지 또는 최소한 귀하의보기에 필요한 것이 무엇인지 그리고 이것이 EF를 사용하는 모델과 다른 점을 설명하지 않고이 질문에 어떻게 대답 할 것이라고 기대하십니까? View 모델을 뷰에 전달하는 것과 관련해서는 이것이 간단해야한다고 생각합니다 :'return View (someInstanceOfAViewModel);'. –
이 쿼리를 반환하도록 변환하는 방법 View (someInstanceOfAViewModel); 네가 쓴 것처럼 나는 이제 내가 잘못 했어. PIf 당신을 위해 열심히, 모델 및 컨트롤러의 작은 예제를 썼습니다. –
모델과 컨트롤러에 넣기 위해 이것을 편집했다고 생각하지만 여전히 불완전하게 보입니다. 허락하신다면 아래의 Craig M이 새로운 모델을 작성해 주셨습니다. 위 모델은 위의 모델에 포함되지 않은 m.imgcrop, m.id, n.commenterName 및 n.comment를 참조합니다. 그래서 당신이 (테이블, 뷰 등에서) 그 데이터를 가져 오는 것을 알기가 어려울 것입니다. db.Photos 및 db.Comments 모델이 누락되었습니다. 위의 모델은 당신이 무엇을하려고하는지 전혀 알려주지 않습니다. – vapcguy