Wrong type of argument MVC C#
Wrong type of argument MVC C#
I'm stuck on a bug since yesterday, in my Return function I have to retrieve all the applications from my database but i got this error :
The model element passed in the dictionary is of type "System.Collections.Generic.List'1[LogViewer.Models.Application]
", but this dictionary requires an element of type "System.Collections.Generic.IEnumerable'1[LogViewer.Controllers.HomeController]
".
System.Collections.Generic.List'1[LogViewer.Models.Application]
System.Collections.Generic.IEnumerable'1[LogViewer.Controllers.HomeController]
Here is my code CrudApplication.cs:
public static IEnumerable<Application> GetApplications()
SqlConnection conn = GetConnexion();
conn.Open();
SqlCommand sql = new SqlCommand("SELECT * from Application", conn);
SqlDataReader dataReader = sql.ExecuteReader();
var listApplications = new List<Application>();
if(dataReader.HasRows)
while (dataReader.Read())
var application = new Application();
application.Name = dataReader["Name"].ToString();
listApplications.Add(application);
conn.Close();
return listApplications;
In my Index Views/Application/Index:
@model IEnumerable<LogViewer.Models.Application>
@
ViewBag.Title = "Index";
<table class="table table-striped tablesorter">
<thead class="thead-inverse">
<tr>
<th id="nameHeader">Name</th>
<th id="urlHeader">URL</th>
<th id="descriptionHeader">Description</th>
</tr>
</thead>
<tbody id="myTable1">
@foreach (var item in Model)
<tr>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.URL)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
</tr>
</tbody>
</table>
My controller ApplicationController.cs:
public ActionResult Index()
Session["ActionName"] = this.ControllerContext.RouteData.Values["action"].ToString();
Session["ControllerName"] = this.ControllerContext.RouteData.Values["controller"].ToString();
return View(CrudApplication.GetApplications());
Solution :
public static IEnumerable<Application> GetApplications()
SqlConnection conn = GetConnexion();
conn.Open();
SqlCommand sql = new SqlCommand("SELECT * from Application", conn);
SqlDataReader dataReader = sql.ExecuteReader();
var listApplications = new List<Application>();
if(dataReader.HasRows)
while (dataReader.Read())
var application = new Application();
application.Name = dataReader["Name"].ToString();
listApplications.Add(application);
conn.Close();
return listApplications.AsEnumerable();
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Aug 22 at 13:26