获取具有特定UserID的所有数据库行的计数,并显示在cshtml页面

时间:2017-12-29 19:05:38

标签: c# asp.net-mvc razor

我正在开发一个包含用户个人资料页面的网站,这些个人资料适用于创作者。在每个创建者页面上,是“收藏”该帐户的能力。当网站用户收藏创建者页面时,我将创建者的ID和喜欢该页面的用户的ID存储在我的数据库中。

在我的主页上,我想显示创建者页面的“收藏夹”总数,我想我需要通过将包含特定创建者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();
    }
}

这是我的主页cshtml,其中显示了创建者帐户:

@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>
}

如何获取收藏夹数据库中包含特定创建者ID的行总数?

编辑我正在添加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 这是我在主页上的当前Debug输出,使用相同的UserID:

enter image description here

1 个答案:

答案 0 :(得分: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);
}

此外,您希望获得Count而不是整个Favorite记录列表。所以,修改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等计数。