0

This is my ActionResult

public ActionResult mytry()
    {
        Empoyee ee = new Empoyee();
        ee.Address = "address1";
        ee.Email = "email1";
        ee.FirstName = "fname";
        ee.Id = 23;
        ee.LastName = "lname1";
        ee.MiddleName = "";
        ee.Salary = 20000;

        db.Empoyees.Add(ee);
        db.SaveChanges();
        return View(ee);
    }

View is

@model IEnumerable<trydbconnectMvc.Models.Empoyee>

@{
ViewBag.Title = "mytry";
}

<h2>mytry</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.MiddleName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Address)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Salary)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Email)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.MiddleName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Address)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Salary)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}

</table>

I am getting error

" The model item passed into the dictionary is of type 'trydbconnectMvc.Models.Empoyee', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[trydbconnectMvc.Models.Empoyee]'."

I am new to mvc. How to solve this?

Bhavin Bhaskaran
  • 572
  • 5
  • 17
  • 41

2 Answers2

2

It seems you're returning only one entity, not a IEnumerable. Just change it in your view:

@model trydbconnectMvc.Models.Empoyee

@{
ViewBag.Title = "mytry";
}

<h2>mytry</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.MiddleName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Address)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Salary)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Email)
    </th>
    <th></th>
</tr>


<tr>
    <td>
        @Html.DisplayFor(modelItem => @Model.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => @Model.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => @Model.MiddleName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => @Model.Address)
    </td>
    <td>
        @Html.DisplayFor(modelItem => @Model.Salary)
    </td>
    <td>
        @Html.DisplayFor(modelItem => @Model.Email)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=@Model.Id }) |
        @Html.ActionLink("Details", "Details", new { id=@Model.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=@Model.Id })
    </td>
</tr>
}

</table>

or if you want, just change in your Controller:

public ActionResult mytry()
    {
        Empoyee ee = new Empoyee();
        ee.Address = "address1";
        ee.Email = "email1";
        ee.FirstName = "fname";
        ee.Id = 23;
        ee.LastName = "lname1";
        ee.MiddleName = "";
        ee.Salary = 20000;

        db.Empoyees.Add(ee);
        db.SaveChanges();

        var list = new List<Empoyee>(){ ee };

        return View(list );
    }

and keep your current view.

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
0

If you must use an IEnumerable, you can make a List<> and pass it in

public ActionResult mytry()
    {
        Empoyee ee = new Empoyee();
        ee.Address = "address1";
        ee.Email = "email1";
        ee.FirstName = "fname";
        ee.Id = 23;
        ee.LastName = "lname1";
        ee.MiddleName = "";
        ee.Salary = 20000;

        db.Empoyees.Add(ee);
        db.SaveChanges();

        List<Employee> employees = new List<Employee>();
        employees.add(ee);
        return View(employees);
    }
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53