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 :



Ienumerable instead List



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();





Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew
Aug 22 at 13:26









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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)