Answer the question
In order to leave comments, you need to log in
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; }
}
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);
}
}
@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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question