Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Able to retrieve data into list but, unable to display data in the view getting below error.

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'MVC_ADO.NET_CRUD.Models.Customer', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVC_ADO.NET_CRUD.Models.Customer]'.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)


What I have tried:

ShowAlCustomerDetails.cshtml

@model IEnumerable<MVC_ADO.NET_CRUD.Models.Customer>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CustomerID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Mobileno)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Birthdate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EmailID)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CustomerID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Mobileno)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Birthdate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.EmailID)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
        }
    </tbody>
</table>

Customer.cs

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MVC_ADO.NET_CRUD.Models;
using MVC_ADO.NET_CRUD.DataAccess;
using System.ComponentModel.DataAnnotations;



namespace MVC_ADO.NET_CRUD.Models
{
    public class Customer
    {
        [Key]

        public int CustomerID { get; set; }



        [Required(ErrorMessage = "Enter Name")]

        public string Name { get; set; }



        [Required(ErrorMessage = "Enter Address")]

        public string Address { get; set; }



        [Required(ErrorMessage = "Enter Mobileno")]

        public string Mobileno { get; set; }



        [DataType(DataType.Date)]

        [Required(ErrorMessage = "Enter Birthdate")]

        public DateTime Birthdate { get; set; }



        [Required(ErrorMessage = "Enter EmailID")]

        public string EmailID { get; set; }
       //  public List<Customer> ShowallCustomer { get; set; }
         public List<Customer> ShowallCustomer { get; set; }


    }
}


Crudcontroller.cs

[HttpGet]
public IActionResult ShowAllCustomerDetails()
{
Customer objCustomer = new Customer();
DataAccessLayer objDB = new DataAccessLayer(); //calling class DBdata
objCustomer.ShowallCustomer = objDB.Selectalldata();
return View(objCustomer);

}

DataAccessLayer.cs

public List<Customer> Selectalldata()
        {
            SqlConnection con = null;

            DataSet ds = null;
            List<Customer> custlist = null;
            try
            {
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ToString());
                SqlCommand cmd = new SqlCommand("Usp_InsertUpdateDelete_Customer", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CustomerID", null);
                cmd.Parameters.AddWithValue("@Name", null);
                cmd.Parameters.AddWithValue("@Address", null);
                cmd.Parameters.AddWithValue("@Mobileno", null);
                cmd.Parameters.AddWithValue("@Birthdate", null);
                cmd.Parameters.AddWithValue("@EmailID", null);
                cmd.Parameters.AddWithValue("@Query", 4);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                ds = new DataSet();
                da.Fill(ds);
                custlist = new List<Customer>();


                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Customer cobj = new Customer();
                    cobj.CustomerID = Convert.ToInt32(ds.Tables[0].Rows[i]["CustomerID"].ToString());
                    cobj.Name = ds.Tables[0].Rows[i]["Name"].ToString();
                    cobj.Address = ds.Tables[0].Rows[i]["Address"].ToString();
                    cobj.Mobileno = ds.Tables[0].Rows[i]["Mobileno"].ToString();
                    cobj.EmailID = ds.Tables[0].Rows[i]["EmailID"].ToString();
                    cobj.Birthdate = Convert.ToDateTime(ds.Tables[0].Rows[i]["Birthdate"].ToString());

                    custlist.Add(cobj);
                }
                return custlist;
            }
            catch
            {
                return custlist;
            }
            finally
            {
                con.Close();
            }
        }
Posted
Updated 22-Sep-19 22:51pm
Comments
Member 12306844 23-Sep-19 2:00am    
Hi there, Can you please help me on this.

1 solution

Your view wants a list of objects as a model

@model IEnumerable<MVC_ADO.NET_CRUD.Models.Customer>


but you are passing it a single object

Customer objCustomer = new Customer();
...
return View(objCustomer);


You need to decide which is correct, the view or the controller. If your view really needs a list why does the controller only send one object? Either change the view to use a single object as a model

@model MVC_ADO.NET_CRUD.Models.Customer


or change the controller to pass the view a list of objects. As you only have one object you'll need to create a list from that one object, but that's fairly pointless.

return View(new List<Customer> { objCustomer });


If think you might be getting confused with the root model object (Customer) and the fact that it has a List as a property (ShowallCustomer). Maybe you only need the view to have Customer as the model and the code in the view then does

@foreach (var item in Model.ShowallCustomer)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900