Невозможно отобразить данные отображения из SQL db в представлении MVC.
Способен извлекать данные в список, но не может отображать данные в представлении, получающем ниже ошибку.
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)
Что я уже пробовал:
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]
общественные IActionResult ShowAllCustomerDetails()
{
Клиент objCustomer = новый клиент();
DataAccessLayer objDB = new DataAccessLayer(); //вызов класса DBdata
objCustomer.ShowallCustomer = objDB.Selectalldata();
смотреть возвращение(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(); } }
Member 12306844
Привет, не могли бы вы мне помочь в этом деле?