Hello Team,
I have encounter this error whiles using ViewModel
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable
Model
This is my controller code
public ActionResult RoomBooking()
{
IEnumerable objBookingViewModel = new List();
var room = objHotelDbEntities.Rooms.ToList();
objBookingViewModel = room.Select(obj=>new SelectListItem
{
Text = obj.RoomNumber,
Value = obj.RoomId.ToString(),
Selected = false
}).ToList();
return View(objBookingViewModel);
}Booking View Model
public class BookingViewModel
{
public string CustomerName { get; set; }
public string PhoneNo { get; set; }
public DateTime BookingFrom { get; set; }
public DateTime BookingTo { get; set; }
public int AssignRoomId { get; set; }
public int NoOfMembers { get; set; }
public decimal TotalAmount { get; set; }
public IEnumerable ListOfRoom { get; set; }
}
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 20, 2023, 1:06 AM EDT
it seems that you're passing a list of
SelectListItemto the view, but your ViewModelBookingViewModelhas properties such as "CustomerName", "PhoneNo", etc. These properties are not present in theSelectListItemclass.try below modified code
public ActionResult RoomBooking() { var roomBookingViewModel = new BookingViewModel(); var room = objHotelDbEntities.Rooms.ToList(); roomBookingViewModel.ListOfRoom = room.Select(obj => new SelectListItem { Text = obj.RoomNumber, Value = obj.RoomId.ToString(), Selected = false }); return View(roomBookingViewModel); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Aug 21, 2023, 6:46 AM EDT
Thank you Ansari, it is working