A
A
Andrey2019-09-30 13:54:12
ASP.NET
Andrey, 2019-09-30 13:54:12

How to overcome type mismatch?

Good afternoon!
I am learning ASP.NET Core 2.1 and doing a test project. Faced such problem: At a conclusion of all employees - there are no problems. Everything is displayed correctly. But when displaying organizations alone, I get an error :(
Please tell me how to avoid the error "InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[System.String]" when passing to View ', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List`1" ?
Kick in the right direction...
Thanks in advance!
Model :

public class Emploee
    {
        public int Id { get; set; }
        public int Num_prop { get; set; }        
        public string Organization { get; set; }
        public string FIO { get; set; }
}

Controller :
public class HomeController : Controller
    {
        ApplicationContext db;

        public HomeController(ApplicationContext context)
        {
            db = context;
        }

        public IActionResult Index()
        {
            var testSelect = db.Emploee                
                .Select(o => o.Organization)
                .Distinct()
                .ToList()                
            
            return View(testSelect);
        }
}

Performance :
@model IEnumerable<TST_net_core_EF_web.Models.Emploee>

@{
    ViewData["Title"] = "Home Page";    
}

<div class="container">
    <table class="table table-striped table-bordered text-center">
        <tr>           
            <td>Организация</td>           
        </tr>
        @foreach (var emp in Model)
        {
            <tr>               
                <td>@emp.Organization</td>                
            </tr>
        }
    </table>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-09-30
@andrey71

Your view accepts a list of employees ( IEnumerable<TST_net_core_EF_web.Models.Emploee>), and you pass List<string>.
That's the whole mistake

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question