I am trying to run this code in asp.net MVC
Controller:
[HttpGet]
public ActionResult TeleList()
{
List<TeleInfo> lstTeleInfo = new List<TeleInfo>();
oyetriviadbEntities db = new oyetriviadbEntities();
lstTeleInfo = db.TeleInfoes.ToList();
return View(lstTeleInfo);
}
.csthml(Razor View Code)
@model IEnumerable<MvcApplication3.Models.MTeleInfo>
@{
ViewBag.Title = "TeleList";
}
<h2>TeleList</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.TeleName)
</th>
<th>
@Html.DisplayNameFor(model => model.TeleMob)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TeleName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TeleMob)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.TeleGUID }) |
@Html.ActionLink("Details", "Details", new { id=item.TeleGUID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.TeleGUID })
</td>
</tr>
}
</table>
Running above code in MVC I am getting the error as below
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication3.TeleInfo]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication3.Models.MTeleInfo]
Let me know what is the issue and how to resolve it?
You are passing the wrong Model if you check the line in controller it is
List<TeleInfo> lstTeleInfo = new List<TeleInfo>();
here in the above code, your model is TeleInfo
, while in the View if you will check
@model IEnumerable<MvcApplication3.Models.MTeleInfo>
Your's razor is expecting List of type MTeleInfo
, so you need to refer the correct Model name.
In the view, it should be
@model IEnumerable<MvcApplication3.TeleInfo>
Change the namespace/model-name according to what it is in your Project.
Your issue is in view, not in C# controller, you need to change the first line of your view, it should be something like
@model IEnumerable<NameSpace.Modelname>
Your model name is not matching with the C# list you are passing to View
existing first line of View is
@model IEnumerable<MvcApplication3.Models.MTeleInfo>
i changed it
@model IEnumerable<MvcApplication3.MTeleInfo>
also got error
what is the namespace i will take
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly