2017-12-29 18 views
1

私はユーザープロファイルページを持つWebサイトを開発しています。これらのプロファイルはクリエイター向けです。各クリエイターページで、アカウントを「お気に入り」にする機能です。ウェブサイトのユーザーがクリエイターのページをお気に入りにすると、作成者のIDとそのページを気に入ったユーザーのIDがデータベースの1行に保存されます。特定のUserIDを持つすべてのデータベース行数を取得し、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内の行の総数を取得するにはどうすればよいですか?

EDIT私が説明するために私の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'の下に2つのデバッグ行を追加して' creatorID'を表示しました。ホームページには2つのCreatorアカウントが表示され、コンソールに2回表示されるcreatorIDは最初のアカウントです。私はオリジナルの投稿を画像で編集しました –

+0

だから、2つの異なるアカウントのお気に入りの数を表示したいですか? – lucky

関連する問題