0

I'm trying to pass movie.Id as a parameter and my IDE is complaining that I have to do a boxing conversion. I've looked up boxing conversions but don't know how to do it in this context. Also I'm trying to pass movies to increment and decrement the page and running into the same problem. The error is happening inside the anchor tag inside the foreach loop.

@using System.Collections
@{
    Layout = null;
    int IncrementPage(MovieResults movies) => movies.Page++;
    int DecrementPage(MovieResults movies) => movies.Page--;
    IEnumerable<MovieResults> movieResults = (IEnumerable<MovieResults>)ViewData["movies"] 
    Root movies = ViewData["movies"] as Root;
    
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>Movie Streaming Availability App</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="css/site.css">
</head>
<body>
<header>
    <div class="container">
        <div class="row">
            <div class="col-4">
                <button type="button" asp-controller="Home" asp-action="Index" style="background: #1b6ec2">Home Page</button>
            </div>
            <div class="col-6">
                <h1 class="text-center">Movie Streaming Availability App</h1>
            </div>
            <div class="dropdown col-4">
                <button class="btn btn-secondary dropdown-toggle" type="button" id="dropDownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="False">Sort by Genres</button>
                <div class="dropdown-menu" aria-labelledby="dropDownMenuButton">
                    <a class="dropdown-item" asp-controller="Movie" asp-action="GetMovies" asp-route-genreId="28">Action</a>
                    <a class="dropdown-item" asp-controller="Movie" asp-action="GetMovies" asp-route-genreId="12">Adventure</a>
                    <a class="dropdown-item" asp-controller="Movie" asp-action="GetMovies" asp-route-genreId="16">Animation</a>
                    <a class="dropdown-item" asp-controller="Movie" asp-action="GetMovies" asp-route-genreId="35">Comedy</a>
                    <a class="dropdown-item" asp-controller="Movie" asp-action="GetMovies" asp-route-genreId="80">Crime</a>
                </div>
            </div>
        </div>
    </div>
</header>
<main>
    <div class="container">
        <div class="row">
            @foreach (var movie in movieResults)
            {
                <div class="col-3">
                    <a asp-controller="Movie" asp-action="MoviePage" asp-route-Id="@movie.Id"><img src="https://image.tmdb.org/t/p/w300/@movie.PosterPath" alt="@movie.Title"></a>
                    <label>@movie.Title</label>
                </div>
            }
        </div>
    </div>
</main>
<footer>
    <div class="container">
        <div class="row">
            <div class="col-3">
                <button type="button" asp-controller="Movie" asp-action="GetMovies" asp-route-page="@(new MovieResults(movies.Page), @IncrementPage(movies))">Next Page</button>
            </div>
            <div class="col-3">
                <button type="button" asp-controller="Movie" asp-action="GetMovies" asp-route-page="@(new MovieResults(movies.Page), @DecrementPage(movies))">Previous Page</button>
            </div>
            <div class="col-3">
                <label class="text-center">Total Number of Pages</label>
                <h3 class="text-center">@(new MovieResults(movies.TotalPages),movies.TotalPages)</h3>
            </div>
            <div class="col-3">
                <label class="text-center">Total Number of Results</label>
                <h3 class="text-center">@(new MovieResults(movies.TotalResults),movies.TotalResults)</h3>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="lib/jquery/dist/jquery.js"></script>
    <script type="text/javascript" src="lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
    <script type="text/javascript" src="~/js/site.js" asp-append-version="true"></script>
</footer>
</body>
</html>
3
  • Of what type is movie.Id? Is it a string or an int returned as object? Commented Dec 25, 2020 at 18:09
  • Oh I'm sorry I forgot to mention it's an int Commented Dec 25, 2020 at 18:21
  • My be I am looking at the wrong place, but in asp-route-Id="@movie.Id" there is no boxing conversion. A boxing conversion would automatically occur if you assigned an int to an object. Commented Dec 25, 2020 at 18:29

1 Answer 1

1

You can to this only, and only if you are sure that the object contains an integer, otherwise it will throw a runtime conversion error exception

For razor @((int)Model.Integer), for code int IncrementPage(MovieResults movies) => ((int)movies.Page)++;

It's never a good ideia to cast objects with no real need, specially inside a foreach loop since boxing/unboxing uses a nice amount of resources to do so. I believe that the best approach is adapt your Model to reflect the "front-end" needs and avoid casting

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.