Reputation: 55
The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[website.Models.main]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List
1[website.Models.List]'
Here i have joined four tables using EF include method.This error occurs while i'm doing this method.
controller :
public IActionResult Index()
{
var listAll = db.main
.Include(x => x.Person)
.ThenInclude(x => x.Entity)
.ThenInclude(x => x.Country)
.ToList();
return View(listAll);
}
View :-
@model List<website.Models.List>
@{
ViewData["Title"] = "Index";
}
Models :- I don't know what i did wrong here please give me any solutions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace website.Models
{
public class List
{
public mainmain{ get; set; }
public Persons Person { get; set; }
public other other{ get; set; }
public Entities Entity { get; set; }
public Countries Country { get; set; }
public int countryId { get; internal set; }
}
public class main
{
public int id{ get; set; }
public int TypeId { get; set; }
public int? PersonId { get; set; }
}
public class Person
{
public Person()
{
main= new HashSet<main>();
}
public int PersonId { get; set; }
public string FirstNameEn { get; set; }
public string FirstNameAr { get; set; }
public string SecondNameAr { get; set; }
public string HomePhonePart1 { get; set; }
public string HomePhonePart2 { get; set; }
public ICollection<main> main{ get; set; }
}
public class Other
{
public int PersonId { get; set; }
public string FatherReligio { get; set; }
public bool? Fatherless { get; set; }
public DateTime? FatherDeathDate { get; set; }
public bool? Motherless { get; set; }
}
public classEntity
{
public Entity()
{
Persons = new HashSet<Persons>();
}
public int CountryId { get; set; }
public string Name { get; set; }
public string ResponsibleName { get; set; }
public string Address { get; set; }
public string Pobox { get; set; }
public string PhonePart1 { get; set; }
public string PhonePart2 { get; set; }
public ICollection<Persons> Persons { get; set; }
}
public class country
{
public country()
{
Entity = new HashSet<Entities>();
Persons = new HashSet<Persons>();
}
public string NameEn { get; set; }
public string NameFr { get; set; }
public string NameSp { get; set; }
public string NameUr { get; set; }
public virtual ICollection<Entities> Entity { get; set; }
public ICollection<Persons> Persons { get; set; }
}
}
Upvotes: 1
Views: 22162
Reputation: 1
PopulateAssignedSpecialtyData(doctor); PopulateDropDownLists();
ViewData["DepartmentId"] = new SelectList(_context.Departments, "Id", "Name");
ViewData["DoctorTypeId"] = new SelectList(_context.DoctorTypes, "Id", "Name");
ViewData["BloodGroupId"] = new SelectList(_context.BloodGroups, "Id", "Name");
return View(doctor);
}
// POST: Doctors/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(
[Bind("ID,FirstName,MiddleName," +
"LastName,CityID,Designation,DepartmentId,DoctorTypeId,Gender,BloodGroupId," +
"HospitalVisitTimeFrom,HospitalVisitTimeTo,HospitalConsultingDuration,OnlineVisitTimeFrom," +
"OnlineVisitTimeTo,onlineConsultingDuration,PresentAssignment," +
"ShortBiography,DoctorEmail,DoctorQualifications,DoctorAchievements,Addresses,DoctorDocuments,DoctorSpecialties,DoctorPositions,DoctorExperiences,DoctorMemberships,")]
Doctor doctor,
string[] selectedOptions,
List<IFormFile> theFiles
)
{
try
{
UpdateDoctorSpecialties(selectedOptions, doctor);
if (ModelState.IsValid)
{
await AddDocumentsAsync(doctor, theFiles);
_context.Add(doctor);
await _context.SaveChangesAsync();
//Create Users when Doctor create in admin login
var userManager = _serviceProvider.GetRequiredService<UserManager<IdentityUser>>();
if (userManager.FindByEmailAsync(doctor.DoctorEmail).Result == null)
{
IdentityUser user = new IdentityUser
{
UserName = doctor.DoctorEmail,
Email = doctor.DoctorEmail
};
IdentityResult result = userManager.CreateAsync(user, "doctor123").Result;
if (result.Succeeded)
{
userManager.AddToRoleAsync(user, "Doctor").Wait();
}
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
catch (RetryLimitExceededException /* dex */)
{
ModelState.AddModelError("", "Unable to save changes after multiple attempts. Try again, and if the problem persists, see your system administrator.");
}
catch (DbUpdateException)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
//Validation Error so give the user another chance.
PopulateAssignedSpecialtyData(doctor);
//Get the full city object for the Doctor and then populate DDL
doctor.City = await _context.Cities.FindAsync(doctor.CityID);
PopulateDropDownLists(doctor);
return View(doctor);
}
// GET: Doctors/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var doctor = await _context.Doctors
.Include(d => d.City)
.Include(d => d.DoctorDocuments)
.Include(d => d.DoctorSpecialties)
.ThenInclude(d => d.Specialty)
.AsNoTracking()
.SingleOrDefaultAsync(d => d.ID == id);
if (doctor == null)
{
return NotFound();
}
PopulateAssignedSpecialtyData(doctor);
PopulateDropDownLists(doctor);
return View(doctor);
}
Upvotes: 0
Reputation: 8459
The listAll
is a list of the main
object, you need to use the corresponding type to accpet it in your razor page:
@model List<website.Models.main>
@{
ViewData["Title"] = "Index";
}
Upvotes: 1
Reputation: 518
try to parse the items into your own List object
public IActionResult Index()
{
var listAll = db.main
.Include(x => x.Person)
.ThenInclude(x => x.Entity)
.ThenInclude(x => x.Country)
.ToList();
List<website.Models.List> newList = new List<website.Models.List>();
foreach(var item in listAll){
website.Models.List listItem = new website.Models.List();
listItem.countryId = item.countryId;
//add your remaining fields
newList.Add(listItem);
}
return View(newList);
}
Upvotes: 2