Using DropDownList to display data from a database

1

I have a form made with MVC and Entity Framework. What I want to do is that using a DropDownList that brings the descriptions of the ratings. The data is obtained from a database, can you give me a hand?

In the controller on line lPeli = db.Peliculas.ToList(); tells me the following error:

  

Severity Code Description Project File Line Deletion status   Error CS0029 The type can not be implicitly converted   'System.Collections.Generic.List' in   'System.Collections.Generic.List' Cinemania C: \ Prog   web 3   2017 \ PW3 \ Cinemania \ Controllers \ AdministracionController.cs 47 Active

How can I solve it?

Controller Code:

public ActionResult Peliculas()
{
    var listaPeliculas = db.Peliculas.ToList();

    return View(listaPeliculas);
}

// GET: Peliculas/NuevoPelicula
public ActionResult NuevoPelicula()
{
    List<Pelicula> lPeli = new List<Pelicula>();
    lPeli = db.Peliculas.ToList();
    ViewBag.listaPeliculas = lPeli;

    return View(lPeli); 
}   

Model:

//    Pelicula.cs
using Cinemania.Models;

namespace Cinemania.Models
{
    [MetadataType(typeof(PeliculaMetadata))]
    public partial class Pelicula
    {

    }    
}

//   PeliculaMetadata.cs
namespace Cinemania.Models
{
    public class PeliculaMetadata
    {
        [Required]      
        public int cod { get; set; }
        [Required]
        [StringLength(150, MinimumLength = 10, ErrorMessage = " El {0} debe tener entre 10 y 50 caracteres")]
        public string Nombre { get; set; }
        [Required]    
        public string Calificacion { get; set; }
        [Required]
        public string Genero { get; set; }
        public string Duracion { get; set; }
    }
}   

View:

@model Cinemania.Peliculas

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>NuevoPelicula</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>

    <!-- Menu -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">CINEMANIA</a>
            </div>
            <div class="collapse navbar-collapse" id="myNavbar">
                <ul class="nav navbar-nav">
                    <li><a href="#">Home</a></li>
                </ul>

                <ul class="nav navbar-nav navbar-right">
                    <li class="active"><a href="#"><span class="glyphicon glyphicon-user"></span> Administracion</a></li>
                    <li><a href="../"><span class="glyphicon glyphicon-log-in"></span> Cerrar Sesion</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        @*using (Html.BeginForm("/NuevoPelicula", "Administracion", new { enctype = "multipart/form-data", @class = "text-danger" }))*@

        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">

                <!-- CABECERA -->
                <h2 class="text-center">Nuevo</h2>

                <!-- INPUT NOMBRE -->
                <div class="form-group">
                    <label for="Nombre">Nombre</label>
                    @Html.TextBoxFor(model => model.Nombre, new { placeholder = "Ingresar Nombre", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
                </div>

                <!-- INPUT DESCRIPCION -->
                <div class="form-group">
                    <label for="descripcion">Descripcion</label>
                    @Html.TextBoxFor(model => model.Descripcion, new { placeholder = "Ingresar Descripcion", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })
                </div>

                <!-- INPUT CALIFICACION -->
                <div class="form-group">

                  <!--  <label for="IdCalificacion">Calificacion</label>
                    <select>
                        <option value="ATP">ATP</option>

                        <option value="May13">May13</option>
                        <option value="May13R">May13R</option>

                        <option value="May16">May16</option>
                        <option value="May16R">May16R</option> 

                    </select>-->
                    @*Html.DropDownList("IdCalificacion", (IEnumerable<SelectListItem>)ViewBag["IdCalificacion"], "Selecione genero", new { @class = "form-control" }) *@

                   @Html.ValidationMessageFor(model => model.IdCalificacion, "", new { @class = "text-danger" })  
                </div>

                <!-- INPUT GENERO -->
                <div class="form-group">
                    <label for="IdGenero">Genero</label>
                    <select>
                        <option value="Terror">Terror</option>
                        <option value="Thriller">Thriller</option>
                        <option value="Accion">Accion</option>
                        <option value="Comedia">Comedia</option>
                        <option value="ComediaR">ComediaR</option>
                    </select>

                    @*Html.DropDownList("IdGenero", (IEnumerable<SelectListItem>)ViewBag["IdGenero"], "Selecione calificacion", new { @class = "form-control" })*@

                    @Html.ValidationMessageFor(model => model.IdGenero, "", new { @class = "text-danger" })
                </div>
                <!-- INPUT IMAGEN -->
                <div class="form-group">
                    <label for="Imagen">Imagen</label>
                    @Html.TextBoxFor(model => model.Imagen, new { Type = "file" })
                    @Html.ValidationMessageFor(model => model.Imagen, "", new { @class = "text-danger" })
                </div>       
                <!-- INPUT DURACION -->
                <div class="form-group">
                    <label for="Duracion">Duracion</label>
                    @Html.TextBoxFor(model => model.Duracion, new { placeholder = "Ingresar Duracion", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Duracion, "", new { @class = "text-danger" })
                </div>

                <!-- BOTON GUARDAR -->
                <div class="text-center">
                    <input type="submit" class="btn btn-success" value="Guardar" />

                    <!-- LINK SALIR -->
                    <a href="./Peliculas" class="btn btn-default">Cancelar</a>
                </div>    
            </div>}
    </div>    
    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
    
asked by Andrés Oporto 19.06.2017 в 00:12
source

2 answers

1

Here is a simple example:

Model

public int Id { get; set; }
public string Nombre { get; set; }
public string Description { get; set; }
public Nullable<int> Capacidad { get; set; }

Controller

List<Curso> lCursos = new List<Curso>();
lCursos = db.Cursos.ToList();
ViewBag.listaCursos = lCursos;

View

@Html.DropDownList("Curso", new SelectList(ViewBag.listaCursos, "ID", "Nombre"))

Now, in your question you pose:

@Html.DropDownList("IdCalificacion", (IEnumerable<SelectListItem>)ViewBag["IdCalificacion"], "Selecione genero", new { @class = "form-control" }) 

Nowhere do I see you use a ViewBag called IdCalificacion .

Your controller

public ActionResult Peliculas()
{
    var listaPeliculas = db.Peliculas.ToList();
    return View(listaPeliculas);
}

This will launch a list as a model in sight, and the view receives @model Cinemania.Peliculas which generates the error:

No se puede convertir implícitamente el tipo 
System.Collections.Generic.List<Cinemania.Peliculas> en
System.Collections.Generic.List<Cinemania.Models.Pelicula>

To solve that, the model should be @model IEnumerable<Cinemania.Peliculas>

    
answered by 19.06.2017 в 20:06
0

I think the problem is that in the controller

// GET: Peliculas/NuevoPelicula
public ActionResult NuevoPelicula()
{
    List<Pelicula> lPeli = new List<Pelicula>();
    lPeli = db.Peliculas.ToList();
    ViewBag.listaPeliculas = lPeli;

    return View(lPeli); 
}   

You send a list of Movies (lPeli) to the view, however in the view you have defined the model as "@model Cinemania.Peliculas" which is a simple Cinemania object.Peliculas no un List

    
answered by 20.06.2017 в 13:10