2016-07-31 5 views
-1

이것은 알림 컨트롤러의 내 색인 작업 코드입니다.asp.net mvc에서 System.Data.Entity.Core.EntityCommandExecutionException을 피하는 방법?

public ActionResult Index() 
     { 
      //gets the current user 
      ApplicationUser currentUser = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 

      //checks if the user is logged in 
      bool val1 = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated; 
      if (val1 == false) 
      { 
       return RedirectToAction("Login", "Account"); 
      } 
      var myNotif = db.Notifications.Where(s => s.User1_Id == currentUser.Id || s.User2_Id == currentUser.Id).Where(s => s.Active_Status != currentUser.Id); 
      ViewBag.Autobots = new NotificationStatus[myNotif.Count()]; 
      int i = 0; 
      foreach(var item in myNotif) 
      { 
       ViewBag.Autobots[i].name = db.Users.Where(x => item.Active_Status == x.Id).First().FirstName + " " + db.Users.Where(x => item.Active_Status == x.Id).First().LastName; 
       i++; 
      } 
      return View(myNotif.ToList()); 
     } 

     private class NotificationStatus 
     { 
      string name; 
     } 

여기에이 오류가 표시됩니다. 어떻게 피할 수 있습니까? 볼 데이터를 보내려고하므로 문자열 배열을 사용하고 있습니다. 내 Linq 쿼리가 잘못된 곳입니까? enter image description here

+1

안녕하세요, 내부 예외를 살펴보십시오. –

+0

어떻게하면됩니까? –

+0

첨부 한 이미지에 링크 복사 예외 세부 사항이 클립 보드에 있습니다. 그것을 클릭 한 다음 텍스트 편집기에 붙여 넣으십시오. –

답변

0
public ActionResult Index() 
{ 
    var userId=User.Identity.GetUserId(); 

    if(!User.Identity.IsAuthenticated) return RedirectToAction("Login", "Account"); 

    var myNotif = db.Notifications 
       .Where(s => s.User1_Id == userId || s.User2_Id == userId) 
       .Where(s => s.Active_Status != userId).ToList(); 

    ViewBag.Autobots = new NotificationStatus[myNotif.Count()]; 
    int i = 0; 
    foreach(var item in myNotif) 
    { 
     var user=db.Users.FirstOrDefault(x=>x.Id==item.Active_Status); 
     ViewBag.Autobots[i].name = user.FirstName + " " + user.LastName; 
     i++; 
    } 
    return View(myNotif.ToList()); 
} 

난 그냥 몇 가지 사소한 변경, 코드를 리팩토링.

목록에서이 오류가 발생하지 않습니다.

public class ViewModel 
{ 
public List<Notification> Notifications {get;set;} 
public NotificationStatus[] NotificationStatusArr {get;set;} 
} 

하고 뷰에 뷰 모델을 보내하지 ViewBags :

내 sugestion이처럼 뷰 모델을 만드는 것입니다!

관련 문제