Issue
I have a really stupid question. I have a foreach loop in my project:
@foreach (var item in Model.HallIndex!)
The foreach gives me all the halls in a cinema + the movies which are scheduled in the future, including the time of the movie. So I get this back from the query to my webapp:
Cinema 1
Now playing:
Next movie: SCREAM (3:00 pm)
Cinema 2:
Now playing:
Next movie: TITANIC (8:30 pm)
Etcetera… I think you get it so far. BUT! I would like to use the Hallnumber to get the result of another query, which gets the movie from the database playing now in each hall. So, I need to get a parameter to the Controller.
Edit
This is what I really try to do, but MovieNow doesn’t work:
Controller:
public async Task<ActionResult<IndexViewModel>> Index(int hallId)
{
IndexViewModel indexViewModel = new IndexViewModel();
indexViewModel.MovieIndex = MovieIndex();
indexViewModel.MovieNow = MovieNow(hallId);
indexViewModel.HallIndex = MovieNext();
return View(indexViewModel);
}
public Showtime MovieNow(int hallId)
{
return _context.Showtime.FromSqlRaw("SELECT * FROM db.\"Showtime\" S JOIN db.\"Movie\" M ON S.\"MovieId\" = M.\"Id\" WHERE \"StartAt\" < now() AND S.\"HallId\" = {0} ORDER BY \"StartAt\" DESC", hallId).FirstOrDefault();
}
ViewModel:
public class IndexViewModel
{
public Hall? Hall { get; set; }
public Movie? Movie { get; set; }
public IEnumerable<Showtime>? MovieIndex { get; set; }
public IEnumerable<Showtime>? HallIndex { get; set; }
public Showtime? MovieNow { get; set; }
}
View:
<div class="col-4">
<div class="text-center index-hall-overview">
@foreach (var item in Model.HallIndex!)
{
<div class="row">
<p class="index-hall-title">CINEMA @Html.DisplayFor(model => item.HallId)</p>
<p class="index-hall-now">Now playing: @Html.DisplayFor(m => Model.MovieNow.Movie.Title, item.HallId) </p>
<p class="index-hall-next"><span class="index-hall-p">Next movie:</span> @Html.DisplayFor(model => item.Movie.Title)
@{
var startTime = item.StartAt.ToLocalTime().TimeOfDay.ToString(@"hh\:mm");
}
(@Html.DisplayFor(model => startTime))</p>
</div>
<hr class="index-hall-hr"/>
}
</div>
</div>
If I do this, I get the error:
"PostgresException: 42702: column reference "Id" is ambiguous POSITION: 8"
Solution
As you are joining the Showtime and Movie tables, the SELECT *
will return all columns from both tables. And apparently both contain an Id column, so which should be used to fill an Id property?
Solution: limit the columns returned to the one correct table by using SELECT s.*
Answered By – Hans Kesting
Answer Checked By – Robin (BugsFixing Admin)