Click here to Skip to main content
15,887,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm on an MVC web application, it takes entities from a separate data Model layer. As I'm working on the entity Movie, the Views use the dataModelLayer.Movie model from the DataModelLayer.

As the Index will show a list of movies, I created a new model which represents a list of movies in order to implement the Index action. I created this new class inside the DataModelLayer.

When I set
ListOfMovies listOfMovies = movies;
in the Index action implementation I got an error, I then let Visual Studio to make a conversion and now an operator appeared on the model class.

How should I implement this? Or should I change the Index action implementation?

What I have tried:

The Index action will show all the movies, implementation ->

public ActionResult Index(string searchTitle)         {

           List<Movie> movies= _movieProxy.GetAllMovies();
           ListOfMovies  listOfMovies = movies;  // <- ListOfMovies is the model

           if (!String.IsNullOrEmpty(searchTitle))
           {
               movie= _movieProxy.GetMovieByOneOrMoreProperties(searchTitle);
           }

               // I pass the ListOfMovie model inside the View to show all movies-->

                       return View(listOfMovies);
           }


I wrote the following code on my ListOfMovies class:

public class ListOfMovies
{
public List<Movie> Movies{ get; set; }
}


but when I set
ListOfMovies listOfMovies = movies;
I got an error in the Index action implementation, so I let Visual Studio make a conversion and now the following code appeared on the model class:

public class ListOfMovies
{
public List<Movie> Movies{ get; set; }

public static implicit operator ListOfMovies(List<Movie> v)
{
  throw new NotImplementedException();
}
Posted
Updated 26-Nov-20 1:57am

1 solution

C#
ListOfMovies  listOfMovies = new ListOfMovies { Movies = movies };

Alternatively, fill in the implementation of your implicit operator:
C#
public static implicit operator ListOfMovies(List<Movie> v)
{
    return new ListOfMovies { Movies = v };
}
 
Share this answer
 
v2
Comments
Richard Deeming 26-Nov-20 8:23am    
You use the parameter passed in to the implicit operator. In this case, the parameter is called v, so you use:
return new ListOfMovies { Movies = v };

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900