How to fix "This dictionary requires a model item of type" in Asp.NET MVC?

asp.net-mvc c# entity-framework-6 model-view-controller

Question

Here is my model class Where is my mistake ?

And error message I received; The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NePisirsem.Models.EntitiyFramework.iletisim]', but this dictionary requires a model item of type 'NePisirsem.Models.Uye'

public partial class iletisim
{
    public int Id { get; set; }
    public string Ad { get; set; }
    public string Email { get; set; }
    public byte Konu { get; set; }
    public string Mesaj { get; set; }
}

My Controller

    PanelEntities db = new PanelEntities();

    // GET: Admin
    public ActionResult Panel()
    {
        var model = db.iletisim.ToList();
        return View(model);
    }

My View

 @model List<NePisirsem.Models.EntitiyFramework.iletisim>
 @foreach (var item in Model)
    {
        <tr>
            <td>@item.Ad</td>
            <td>@item.Email</td>
            <td>@item.Konu</td>
            <td>@item.Mesaj</td>
        </tr>
    }
1
2
1/16/2019 7:55:15 PM

Accepted Answer

You just have to make sure that the model passed in view is compatible(same as passed model) with the model being used inside View,

Compare the namespace where both classes are defined they should be same.Classes should be same

Example:

if you are using

NePisirsem.Models.EntitiyFramework.iletisim

inside view as model then your class iletisim should be like this inside Model folder

namespace EntityFramework
{

 public partial class iletisim
 {
public int Id { get; set; }
public string Ad { get; set; }
public string Email { get; set; }
public byte Konu { get; set; }
public string Mesaj { get; set; }
 }

}

You can also check if both classes are same by using type check

    Type viewModel = typeof(List<NePisirsem.Models.EntitiyFramework.iletisim>());
    Type controllerModel = typeof(iletisim);

  if(!controllerModel.Equals(viewModel))
   //MOdel not matches
1
1/16/2019 8:14:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow