locked
C# Asp net core MVC, Extracting data from a Model list “List<Movie>” in a View RRS feed

  • Question

  • User-1016473172 posted

    I want to Create a "max price" filter system from the Asp net core 3.1 "MvMovie" tutorial project. this is the View/movies/Index.cshtml

    @model MvcMovie.Models.MovieGenreViewModel
    

        <select asp-for="MovieGenre" asp-items="Model.Genres">
            <option value="">All</option>
        </select>
    
        Title: <input type="text" asp-for="SearchString" />
        <input type="submit" value="Filter" />
    
        Max price <input type="text" asp-for="Movies" />
        <input type="submit" value="Filter" />
    </p>
    

    This is what I have added

    Max price <input type="text" asp-for="Movies.price" />
        <input type="submit" value="Filter" />
    

    The problem is that I cannot recieve the price from the Movie class ( Models/movies)

     public class Movie
    {
        public int Id { get; set; }
    
        [StringLength(60, MinimumLength = 3)]
        [Required]
        public string Title { get; set; }
    
        [Display(Name = "Release Date")]
        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }
    
        [Range(1, 100)]
        [DataType(DataType.Currency)]
        [Column(TypeName = "decimal(18, 2)")]
        public decimal Price { get; set; }
    
        [RegularExpression(@"^[A-Z]+[a-zA-Z]*$")]
        [Required]
        [StringLength(30)]
        public string Genre { get; set; }
    
        [RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$")]
        [StringLength(5)]
        [Required]
        public string Rating { get; set; }
    }
    

    Because it is using the @model MvcMovie.Models.MovieGenreViewModel Models/MovieGenreViewModel.cs:

        public class MovieGenreViewModel
    {
        public List<Movie> Movies { get; set; }
        public SelectList Genres { get; set; }
        public string MovieGenre { get; set; }
        public string SearchString { get; set; }
    
    }
    

    I know the "Price" data is in the List

    List<Movie>
    

    but how do I extract it and put it in the View/movies/Index.cshtml so I can work on my price filter system.

    Tuesday, November 3, 2020 9:31 PM

All replies

  • User475983607 posted

    Max price is a filter.  Add a MaxPrice input and MaxPrice property to the View Model.  Then use the MaxPrice to filter the Move list.

    Tuesday, November 3, 2020 10:14 PM
  • User-1016473172 posted

    Max price is a filter.  Add a MaxPrice input and MaxPrice property to the View Model.  Then use the MaxPrice to filter the Move list.

    I have already done that :

    Max price: <input type="text" asp-for="price" />
            <input type="submit" value="Filter" />

    my question is how do i get asp-for="price" linked to the price data in the Movie.cs model 
    without changing:

    @model MvcMovie.Models.MovieGenreViewModel

    the MovieGenreViewModel does have a List<movie> option 

    how do i get the Price data from that List<movie> and use it in the view

    like something like this: 

    Max price: <input type="text" asp-for="Movie.price" /> <input type="submit" value="Filter" />



    Tuesday, November 3, 2020 10:26 PM
  • User475983607 posted

    I have already done that :

    Max price: <input type="text" asp-for="price" />
            <input type="submit" value="Filter" />

    Maybe I misunderstand but I though Price is the property to be filtered.   MaxPrice is the filter applied to Price.

    Tuesday, November 3, 2020 10:31 PM
  • User-1016473172 posted

    arclite860

    I have already done that :

    Max price: <input type="text" asp-for="price" />
            <input type="submit" value="Filter" />

    Maybe I misunderstand but I though Price is the property to be filtered.   MaxPrice is the filter applied to Price.

    no "max price" is just some text that I came up with its not a code or anything look:

    <form asp-controller="Movies" asp-action="Index" method="get">
        <p>
    
            <select asp-for="MovieGenre" asp-items="Model.Genres">
                <option value="">All</option>
            </select>
    
            Title: <input type="text" asp-for="SearchString" />
            <input type="submit" value="Filter" />
    
            Max price: <input type="text" asp-for="Movies" />
            <input type="submit" value="Filter" />
        </p>
    </form>

    Just as "Title : " its the text before the input 

    Tuesday, November 3, 2020 10:34 PM
  • User1034446946 posted

    something like

    var orderedMovies = movies.OrderBy(o => t.Price);

    var maxPrice = orderedMovies.First().Price;
    var minPrice = orderedMovies.Last().Price;

    you would need to do validation tomake sure there are movies and there is more than 1, you could also use a slider with the two values.

    Tuesday, November 3, 2020 11:59 PM
  • User-1016473172 posted

    something like

    var orderedMovies = movies.OrderBy(o => t.Price);

    var maxPrice = orderedMovies.First().Price;
    var minPrice = orderedMovies.Last().Price;

    you would need to do validation tomake sure there are movies and there is more than 1, you could also use a slider with the two values.

     i know but that part is in the controller, but my question is, how do i let the view (index.cshtml)  read the price data as this data is in the movie.cs and not in the MovieGenreViewModel.cs 

    its about this part right here that i cant figure out:

    <input type="text" asp-for"="Movies" asp-items="price" />
           

    because i cant acces asp-item="price" because im using this model class :

    @model MvcMovie.Models.MovieGenreViewModel

    the above class only containts a List<movie> data
    so how can i acces the "price" data from the movie.cs

    Wednesday, November 4, 2020 1:19 AM
  • User1034446946 posted

    it all depends on what you want to do with the value, which is unclear, because the value by itself means nothing.

    If your wanting to put the value in the text box you add the value field to the input element

    you can add additional properties to the view model

    public decimal MaxPrice => this.Movies.OrderBy(o => o.Price).First().Price;

    you also want a different view model for the search submission, which will like change the need viewmodel your sending with all the other information, you could also then split the view up into partials.

    Wednesday, November 4, 2020 1:45 AM
  • User1312693872 posted

    Hi,arclite860

    arclite860

    so how can i acces the "price" data from the movie.cs

    Do you want to get the price data from the List<Movie> in ViewModel?

    And I assume you want to edit the price according to the data you choose by using ViewModel?

    If so, I made a demo for you, you can check it:

    Model:

    public class Movie
        {
            public int Id { get; set; }
    
            [StringLength(60, MinimumLength = 3)]
            [Required]
            public string Title { get; set; }
    
    
            [Range(1, 100)]
            [DataType(DataType.Currency)]
            [Column(TypeName = "decimal(18, 2)")]
            public decimal Price { get; set; }
        }
     public class MovieGenreViewModel
        {
            public List<Movie> Movies { get; set; }
        }

    Controller:

    public IActionResult Index()
            {
                var model = new MovieGenreViewModel
                { 
                    Movies = _db.Movies.ToList()
                };
                return View(model);
            }
    
            public async Task<IActionResult> Edit(int? id)
            {
                Movie movie = _db.Movies.Find(id);
                MovieGenreViewModel movieGenreViewModel = new MovieGenreViewModel()
                {     Movies=new List<Movie> { movie}        
                };
                return View(movieGenreViewModel);
            }

    Index view:

    @model MovieGenreViewModel
    
    @foreach (var item in Model.Movies)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a>
            </td>
        </tr>
    }

    Edit View:

    @model MovieGenreViewModel
    <h1>Edit</h1>
    <h4>Movie</h4>
    Max Price:
    @foreach (var item in Model.Movies)
    {
        <tr>
            <td>
                <input type="text" asp-for="@item.Price" />
            </td>
        </tr>
    }
    

    Result:

    Best Regards,

    Jerry Cai

    Wednesday, November 4, 2020 9:52 AM