【问题标题】:I want to show upcoming gigs announcements in my home page我想在我的主页上显示即将发布的演出公告
【发布时间】:2019-12-27 19:11:38
【问题描述】:

enter image description hereenter image description here这是我的家庭控制器:

public class HomeController : Controller
    {
        private ApplicationDbContext _context;

        public HomeController()
        {
            _context = new ApplicationDbContext();
        }
        
        public ActionResult Index()
        {
            var upcomingGigs = _context.Gigs
                .Include(g => g.Artist)
                .Include(g => g.Genre)
                .Where(g => g.DateTime > DateTime.Now);

            return View(upcomingGigs);
        }
    @foreach(模型中的 var gig) {
  • @gig.DateTime.ToString("MMM") @gig.DateTime.ToString("d") @gig.Artist.Name
    @model IEnumerable<BigHub.Models.Gig>
    @{
        ViewBag.Title = "Home Page";
    
    }
    
    
    
    <ul>
    
    
        @foreach (var gig in Model)
         {
            <li>
                <div class="date">
                    <div class="month">
                        @gig.DateTime.ToString("MMM")
                    </div>
                    <div class="day">
                        @gig.DateTime.ToString("d ")
                    </div>
    
                </div>
    
                <div class="details">
                    <span class="artist">
                        @gig.Artist.Name
                    </span>
                    <span class="genre">
                        @gig.Genre.Name
                    </span>
    
                </div>
            </li>
         }
    
    
    </ul>
  • }

此代码有效,但我得到了 输出为空。希望有人能帮忙。

这是我的预期输出

这是我的输出

【问题讨论】:

  • 您能否详细说明您想要完成的任务,例如插图或现场示例?
  • 请展示你到目前为止所做的尝试。
  • 我正在调试但得到空的
      列表。

    标签: html asp.net-mvc-5


    【解决方案1】:

    首先,您需要在末尾添加 ToList()。试试这个:

    var upcomingGigs = _context.Gigs
                    .Include("Artist")
                    .Include("Genre").ToList();
    

    其次,如果您的 where 子句尝试对属于 Include() 内的表的列执行过滤,则它是不正确的。

    我建议像这样创建一个 ViewModel:

    public class Movie_ViewModel
    {
        public string genreName { get; set; }
        public string artistName { get; set; }
        public string movieDateTime { get; set; }
    }
    

    现在,在 Controller 中,假设 DateTime column 属于 Gigs table

    List<Movie_ViewModel> upcomingGigs = _context.Gigs.Where(g => g.DateTime > DateTime.Now)
                    .Include("Artist")
                    .Include("Genre")
                    .Select( m => new Movie_ViewModel{
                                              genreName = m.Genre.Name,
                                              artistName = m.Artist.Name,
                                              movieDateTime = m.DateTime
                          })
                    .ToList();
    
    return View(upcomingGigs);
    

    最后,在你看来:

    @model IEnumerable<Path.To.Movie_ViewModel>
    @foreach (var item in Model)
    {
        item.genreName  . . . 
    }
    

    希望这有帮助:)

    【讨论】:

      猜你喜欢
      • 2020-06-18
      • 2019-06-11
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多