locked
Cannot implicitly convert type System.Collections.Generic.IEnumerable<LMS.Models.Tareas> to System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>. An explicit conversion exists RRS feed

  • Question

  • User-1648867167 posted

    Hi, I'm learning asp.net mvc and I have this error:

    Cannot implicitly convert type System.Collections.Generic.IEnumerable<LMS.Models.Tareas> to System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem>. An explicit conversion exists

    I want to retrieve information from tareas database in a dropdownlist,the error is presented in the view

    I have this view model:

     public class CalificacionyTareaViewModel
    {
        public IEnumerable<Tareas> TareasList { get; set; }
    
    
    
        public Calificaciones Calificaciones { get; set; }
    
        public IEnumerable<Estudiantes> EstudiantesList { get; set; }
    
    
        public string StatusMessage { get; set; }
    
    
    
    }

    I have a method called Create:

    public async Task<IActionResult> Create()
        {
    
    
    
    
    
    
            CalificacionyTareaViewModel model = new CalificacionyTareaViewModel()
            {
                TareasList = await _db.Tareas.ToListAsync(),
    
                Calificaciones = new Models.Calificaciones(),
    
    
    
    
                EstudiantesList = await _db.Estudiantes.ToListAsync()
    
    
    
    
            };
    
    
    
            return View(model);
        }

    And I have a view called create:

    <form method="post" asp-action="Create">
    <div class="border backgroundWhite">
    
        <div asp-validation-summary="ModelOnly" class="text-danger">
    
    
        </div>
        <div class="form-group row">
    
            <div class="col-3">
    
                <label asp-for="Calificaciones.TareaId" class="col-form-label"></label>
    
    
            </div>
    
            <div class="col-5">
    
                <select  asp-for="Calificaciones.TareaId" asp-items="Model.TareasList" class="form-control"></select>
            </div>
    
    
        </div>
    
    
    
    
        <div class="form-group row">
    
            <div class="col-5 offset-2">
    
                <partial name="_CreateAndBackToListButton" />
    
            </div>
    
        </div>
    
    </div>

    How I can solve this problem?

    Friday, August 14, 2020 1:49 AM

Answers

  • User-821857111 posted

    The asp-items attribute expects an IEnumerable<SelectListItem> or a SelectList as an argument. 

    https://www.learnrazorpages.com/razor-pages/tag-helpers/select-tag-helper

    You have passed in an IEnumerable<Tareas>. ASP.NET Core cannot convert that to the required type, hence the error message. You have to ASP.NET how to convert it by passing it to the constructor of a SelectList, specifying the property to use for the Value, and the one to use for the Text (I've used TareaName as an example) of each option :

    asp-items="@(new SelectList(Model.TareasList, "TareaId", "TareaName"))"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 14, 2020 5:49 AM