I have this problem : InvalidOperationException: The model item passed into the ViewDataDictionary is of type ‘System.Collections.Generic.List`1[BookingPortal.Models.ViewModels.WarehouseViewModel]’, but this ViewDataDictionary instance requires a model item of type ‘BookingPortal.Models.ViewModels.WarehouseViewModel’.
Here is my Controller:
public IActionResult B2bBookingInsert()
{
ViewData["Title"] = "New Booking";
List<WarehouseViewModel> warehouse = bookingPortalService.
GetWarehousesToChoseForInsert();
return View("B2bWarehouseSelection" , warehouse);
}
My Application Service :
public List<WarehouseViewModel> GetWarehousesToChoseForInsert()
{
string query = "SELECT * from Warehouse";
DataSet dataset = db.Query(query);
var dataTable = dataset.Tables[0];
var warehouseList = new List<WarehouseViewModel>();
foreach (DataRow warehouseRow in dataTable.Rows) {
WarehouseViewModel warehouse = WarehouseViewModel.FromDataRow(warehouseRow);
warehouseList.Add(warehouse);
}
return warehouseList;
}
My ViewModel “WarehouseViewModel :
namespace BookingPortal.Models.ViewModels;
public class WarehouseViewModel
{
public int IdWarehouse {get; set;}
public int Client {get; set;}
public string? WarehouseCode {get; set;}
public string? Descritpion {get; set;}
public string? Street {get; set;}
public string? Locality {get; set;}
public string? Cap {get; set;}
public string? City {get; set;}
public string? Nation {get; set;}
public string? Telephone {get; set;}
public string? Email {get; set;}
public string? ActiveWeb {get; set;}
public string? Active {get; set;}
public string? Interlocutor {get; set;}
public string? Latitude {get; set;}
public string? Longitude {get; set;}
public string? ActiveLoad {get; set;}
public string? ActiveUnload {get; set;}
public string? WarehouseInformationInSelect {get; set;}
public static WarehouseViewModel FromDataRow(DataRow warehouseRow)
{
var warehouseViewModel = new WarehouseViewModel {
IdWarehouse = Convert.ToInt32(warehouseRow["IdWarehouse"]),
Client = Convert.ToInt32(warehouseRow["Client"]),
WarehouseCode = Convert.ToString(warehouseRow["WarehouseCode"]),
Descritpion = Convert.ToString(warehouseRow["Descritpion"]),
Street = Convert.ToString(warehouseRow["Street"]),
Locality = Convert.ToString(warehouseRow["Locality"]),
Cap = Convert.ToString(warehouseRow["Cap"]),
City = Convert.ToString(warehouseRow["City"]),
Nation = Convert.ToString(warehouseRow["Nation"]),
Telephone = Convert.ToString(warehouseRow["Telephone"]),
Email = Convert.ToString(warehouseRow["Email"]),
Interlocutor = Convert.ToString(warehouseRow["Interlocutor"]),
Latitude = Convert.ToString(warehouseRow["Latitude"]),
ActiveLoad = Convert.ToString(warehouseRow["ActiveLoad"]),
ActiveUnload = Convert.ToString(warehouseRow["ActiveUnload"]),
WarehouseInformationInSelect = Convert.ToString(warehouseRow["Descritpion"]) + " , " + Convert.ToString(warehouseRow["Street"]) + " , " + Convert.ToString(warehouseRow["Locality"]) + " , " + Convert.ToString(warehouseRow["City"]) + " , " + Convert.ToString(warehouseRow["Cap"])
};
return warehouseViewModel;
}
}
My Content View “B2bWarehouseSelection” : I’ve kept it to a minimum to present the problem to you
@model List<WarehouseViewModel>
@{
ViewData["Title"] = "New Booking";
}
@foreach (var Item in Model) {
<div>Warehouse</div>
}
I expect the view to open normally since I’m presenting a type like the model it’s typed on
MarcoD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.