2017-12-29 5 views
1

사용자 프로필 페이지가있는 웹 사이트를 개발 중입니다.이 프로필은 제작자 용입니다. 각 작성자 페이지에서 계정을 "즐겨 찾기"하는 기능이 있습니다. 웹 사이트 사용자가 작성자 페이지를 좋아할 때, 필자는 작성자의 ID와 페이지를 좋아하는 사용자의 ID를 데이터베이스의 단일 행에 저장합니다.특정 사용자 ID를 가진 모든 데이터베이스 행 수를 표시하고 cshtml 페이지에 표시

내 홈페이지에 작성자 페이지의 "즐겨 찾기"총계를 표시하고 특정 작성자 ID를 포함하는 모든 행을 목록에 합산하여이를 수행해야한다고 생각했습니다. Count - 그 중 면도기에서. 현재로서는 작성자 계정이 내 홈 페이지에 표시되어 있지만 컨트롤러에서 creatorID를 얻을 수있는 방법을 모르므로 getAllFavoritesByCreatorID(string creatorID)으로 데이터베이스의 모든 행을 찾을 수 있습니다.

홈 컨트롤러 : 여기 내 코드입니다

public ActionResult Index() 
{ 
    var model = new HomeViewModel(); 
    model.FeaturedAlbum = EntityDataAccess.GetCurrentFeaturedAlbum(); 
    if(model.FeaturedAlbum == null) 
    { 
     model.FeaturedAlbum = new FeaturedAlbum(); 
     model.FeaturedAlbum.Album = new Album(); 
     model.FeaturedAlbum.Album.AccountInfo = new AccountInfo(); 
    } 
    //here's where relevant code starts 
    model.InspiredCreator = EntityDataAccess.GetAllCreatorAccountInfo(); 
    model.Favorite = EntityDataAccess.GetAllFavoritesByCreatorID(need a input arg here); 
    model.TrendingSongs = EntityDataAccess.GetTopPlayedSongsByCount(51).ToList(); 
    return View(model); 
} 

홈 모델 :

public class HomeViewModel 
{ 
    public FeaturedAlbum FeaturedAlbum { get; set; } 
    public List<Song> TrendingSongs { get; set; } 
    //The AccountInfo class contains the creator UserID 
    public List<AccountInfo> InspiredCreator { get; set; } 
    //the Favorite class contains the creatorID and the userID of the user that favorited the page 
    public List<Favorite> Favorite { get; set; } 
} 

EntityDataAccess.cs이 내가 특정 CreatorID와 DB의 모든 행을 찾기 위해 노력하고 방법은 다음과 같습니다

public static List<Favorite> GetAllFavoritesByCreatorID(string creatorID) 
{ 
    using (var Context = GetContext()) 
    { 
     return Context.Favorites.Where(x => x.CreatorID == creatorID).ToList(); 
    } 
} 
public static List<AccountInfo> GetAllCreatorAccountInfo() 
{ 
    using (var Context = GetContext()) 
    { 
     return Context.AccountInfoes.Where(x => x.CreatorFL == true).OrderBy(o => Guid.NewGuid()).Take(2).ToList(); 
    } 
} 

작성자 계정을 표시하는 내 홈 페이지는 다음과 같습니다.

@foreach (var item in Model.InspiredCreator) 
{ 
    //the following item.UserID is the creator ID I need on controller side 
    <a href="@Url.Action("Index", "Creator", new { ID = item.UserID })"> 
    <div class="feat-creators-indi"> 
     <div class="creator-img-container"> 
      <img class="creator-img" src="@(item.ImageURL != null ? (item.ImageURL) + "-/format/jpeg/-/quality/lightest/" : "https://unearthapp.io/resources/images/artist_ph.jpg")" alt="@item.DisplayName Creator" /> 
     </div> 
     <div class="creator-bio-container"> 
      <div class="creator-txt-center"> 
       <div class="creator-name">@item.DisplayName</div> 
       <div class="creator-bio">@item.Bio</div> 
       <div class="creator-followers">//NEED TO INSERT FOLLOWER COUNT HERE</div> 
      </div> 
     </div> 
    </div> 
    </a> 
} 

특정 creatorID가 포함 된 즐겨 찾기 db의 총 행을 어떻게 얻을 수 있습니까?

편집AccountInfo.csFavorite.cs 설명하기 위해 추가 해요 :

public partial class AccountInfo 
{ 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public AccountInfo() 
    { 
     this.Albums = new HashSet<Album>(); 
     this.DownloadHistories = new HashSet<DownloadHistory>(); 
     this.Spotlights = new HashSet<Spotlight>(); 
     this.Videos = new HashSet<Video>(); 
    } 


    public int AccountInfoID { get; set; } 
    public string UserID { get; set; } 
    public string DisplayName { get; set; } 
    public string Email { get; set; } 
    public string ImageURL { get; set; } 
    public string FacebookURL { get; set; } 
    public string TwitterURL { get; set; } 
    public string InstagramURL { get; set; } 
    public string SpotifyURL { get; set; } 
    public string iTunesURL { get; set; } 
    public string YouTubeURL { get; set; } 
    public string WebURL { get; set; } 
    public string MerchURL { get; set; } 
    public string Bio { get; set; } 
    public Nullable<bool> AllowCollaborationFL { get; set; } 
    public bool VerifiedFL { get; set; } 
    public bool NeedsVerificationFL { get; set; } 
    public bool AdminFL { get; set; } 
    public bool LabelFL { get; set; } 
    public bool CreatorFL { get; set; } 

    public virtual AspNetUser AspNetUser { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Album> Albums { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<DownloadHistory> DownloadHistories { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Spotlight> Spotlights { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Video> Videos { get; set; } 

    } 

} 

다른 클래스 :

public partial class Favorite 
{ 

    public int Id { get; set; } 
    //ID of user that liked the page 
    public string UserID { get; set; } 
    //creatorID of the page the user liked 
    public string CreatorID { get; set; } 
    public Nullable<int> Liked { get; set; } 

} 

편집을 2 여기 내 현재의 디버그 출력하는 경우는에의 동일한 사용자 ID가 사용됩니다.

enter image description here

답변

1

귀하의 질문을 올바르게 이해 한 경우 컨트롤러 작업에서 UserID 개의 계정을 가져오고 싶습니다.

model.InspiredCreator = EntityDataAccess.GetAllCreatorAccountInfo(); 
model.FavoriteCounts = new List<AccountInfoFavoriteCount>(); 
foreach (var creator in model.InspiredCreator) 
{ 
    var favoriteCount = new AccountInfoFavoriteCount() 
    { 
     CreatorID = creator.UserID 
    }; 
    favoriteCount.FavoriteCount = EntityDataAccess.GetAllFavoritesByCreatorID(creator.UserID); 
    model.FavoriteCounts.Add(favoriteCount); 
} 

또한, 대신 Favorite 기록의 전체 목록의 Count을 얻을합니다. 그래서, EF 방법을 수정하십시오;

public static int GetAllFavoritesByCreatorID(string creatorID) 
{ 
    using (var Context = GetContext()) 
    { 
     return Context.Favorites.Where(x => x.CreatorID == creatorID).Count(); 
    } 
} 

그런 다음 모델을 변경하십시오.

public class HomeViewModel 
{ 
    public FeaturedAlbum FeaturedAlbum { get; set; } 
    public List<Song> TrendingSongs { get; set; } 
    public List<AccountInfo> InspiredCreator { get; set; } 
    public List<AccountInfoFavoriteCount> FavoriteCounts { get; set; } //Change this line 
} 

public class AccountInfoFavoriteCount 
{ 
    public int FavoriteCount { get; set; } 

    public string CreatorID { get; set; } 
} 

마지막으로보기에서 Model.FavoriteCounts과 같은 수를 얻을 수 있습니다.

+0

나는 우리가 바른 길에 있다고 생각합니다. 내 creatorId는 문자열이므로'creatorId = ""'로 초기화했습니다. 어떻게 이것을 지금 내 cshtml에 통합 할 수 있습니까? –

+0

가까이 오면 FirstOrDefault를 사용하고 있기 때문에 컨트롤러에서 동일한 userID를 사용하고 있다고 생각합니다. creatorId = item.UserID' 아래에 두 개의 디버그 라인을 추가하여'creatorID'를 출력했습니다.홈 페이지에는 두 개의 작성자 계정이 표시되고 콘솔에서 두 번 보인 작성자 ID는 첫 번째 계정입니다. 이미지로 원래 게시물을 편집했습니다. –

+0

그래서 두 개의 다른 계정의 즐겨 찾기 개수를 표시 하시겠습니까? – lucky

관련 문제