1

I tried this code but I have error like this:

The model item passed into the dictionary is of type 
'System.Collections.Generic.List`1[XNet.Repository.Model.RoomType]', 
but this dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable`1[XNet.Repository.Model.EditRoomTypeViewModel]'.

I don't know, whats part give an error. Please help.

my service

public List<EditRoomTypeViewModel> GetViewRoom(int RoomTypeID)

{
    List<RoomType> roomTypes = (from d in _RoomTypeRepository.All()
                          select d).ToList();

    List<EditRoomTypeViewModel> editRoomTypeViewModel = new List<EditRoomTypeViewModel>();

    foreach (RoomType roomType in roomTypes)
    {
        editRoomTypeViewModel.Add(new EditRoomTypeViewModel
        {
            RoomTypeID = RoomTypeID,
            RoomTypeName = roomType.RoomtypeName,
            RoomTypeDescription = roomType.RoomTypeDescripton,
        });
    }

    return editRoomTypeViewModel;
}

my controller

public ActionResult Room()
        {
            ViewBag.hotel = _hotelService.GetByID(2).HotelName;

            List<EditRoomTypeViewModel> editRoomTypeViewModel = _roomViewService.GetViewRoom(_HotelID);
            return View(editRoomTypeViewModel.FirstOrDefault());
        }

my view model

 public class EditRoomTypeViewModel
    {
        public int RoomTypeID { get; set; }
        public string RoomTypeName { get; set; }
        public string RoomTypeDescription { get; set; }

    }

my view

@model IEnumerable<XNet.Repository.Model.EditRoomTypeViewModel>
@{
    ViewBag.Title = "Room";
}

<h2>Room</h2>
<div>
    @Html.Label("Hotel Name");
</div>

<div>
   @ViewBag.hotel
</div>

<table>
    @foreach (var a in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => a.RoomTypeName)
            </td>
            <td>
               <input style="width:100px;" type="button" title="EditRoomType" value="Edit"  onclick="location.href='@Url.Action("EditRoom", "Hotel", new { RoomTypeID = a.RoomTypeID})'" />
            </td>
        </tr>
    }
</table>

<input style="width:200px;" type="button" title="EditRoomType" value="New Room Type"  onclick="location.href='@Url.Action("NewRoom", "Hotel")    '" />
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
novian kristianto
  • 751
  • 3
  • 12
  • 34

2 Answers2

0

I noticed that you returned just one editRoomTypeViewModel object in your controller, but in your view you declared the model as IEnumerable<XNet.Repository.Model.EditRoomTypeViewModel>.

Another point is that the error seems to be related to an assignment of ViewBag somewhere else, cause it contains thisdictionaryrequires a model item of type and probablt the only thing that is of type dictionary is ViewBag.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • take a look at http://stackoverflow.com/questions/650393/renderpartial-with-null-model-gets-passed-the-wrong-type also. – Jahan Zinedine May 29 '13 at 06:54
  • if i use this @model XNet.Repository.Model.EditRoomTypeViewModel , i cant use foreach to loop my room. so, how i can solve this? thanks for help me – novian kristianto May 29 '13 at 06:54
  • 1
    If you want to loop rooms then you have to pass the list from Controller not FirstOrDefault(). return View(editRoomTypeViewModel.FirstOrDefault()); – Diya Khan May 29 '13 at 06:56
  • 2
    So change the `return View(editRoomTypeViewModel.FirstOrDefault())` to `return View(editRoomTypeViewModel)`. – Jahan Zinedine May 29 '13 at 06:56
  • can you explain me, what different of FirstOrDefault, First, and not use both of them? – novian kristianto May 29 '13 at 06:59
  • 1
    If we want to select the entire list items then we dont use these methods. For their differences, take a look at http://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq – Diya Khan May 29 '13 at 07:10
0

Just remove the .FirstOrDefault() in the controller action and you should be good to go.

public ActionResult Room()
{
    ViewBag.hotel = _hotelService.GetByID(2).HotelName;

    List<EditRoomTypeViewModel> editRoomTypeViewModel = _roomViewService.GetViewRoom(_HotelID);
    return View(editRoomTypeViewModel);
}
wysinawyg
  • 280
  • 2
  • 13