kkakkurt
kkakkurt

Reputation: 2800

Data from AspNetUsers table error and "System.Collections.Generic.IEnumerable" error

I want to list UserId and UserName columns from AspNetUsers table into my Admin Area's EditProfile view.

My EditProfile model is:

namespace AdminPanelExercises1.Areas.admin.Models
{
   public class EditProfileModel
   {
      public int ID { get; set; }
      public string UserID { get; set; }
      public string UserName { get; set; }
      public string UserEmail { get; set; }
      public bool IsAdmin { get; set; }
   }
}

My EditProfile controller is:

private AdminPanelExercises1.Models.ApplicationUser db3 = new AdminPanelExercises1.Models.ApplicationUser();

    // GET: /admin/EditProfile/
    public ActionResult Index()
    {

        EditProfileModel EPM = new EditProfileModel();
        EPM.UserID = db3.Id;
        EPM.UserName = db3.UserName;

        return View(EPM);
    }

My EditProfile view is:

@model IEnumerable<AdminPanelExercises1.Areas.admin.Models.EditProfileModel>

@{
   ViewBag.Title = "Account Management";
   Layout = "~/Areas/admin/Views/Shared/_layout.cshtml";
 }

 <h2>Index</h2>

 <p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserID)
        </th>
    <th>
        @Html.DisplayNameFor(model => model.UserName)
    </th>

    <th></th>
    </tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </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>

And also I'm getting error like:

The model item passed into the dictionary is of type 'AdminPanelExercises1.Areas.admin.Models.EditProfileModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AdminPanelExercises1.Areas.admin.Models.EditProfileModel]'.

How can I fix this problem? Am I writting wrong code for getting data from AspNetUsers table?

Upvotes: 0

Views: 445

Answers (1)

Habib
Habib

Reputation: 223237

Your View is expecting a model of IEnumerable (IEnumerable<AdminPanelExercises1.Areas.admin.Models.EditProfileModel>) and you are returning a single Item from your Controller.

Your View should have a model like:

@model AdminPanelExercises1.Areas.admin.Models.EditProfileModel

If you are going to show multiple records in your View then your controller should return multiple items (Not a single item, like the current code)

Upvotes: 2

Related Questions