0

i try to show my data using viewmodel, but i have error like this

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

how i can solve my problem?? this my code

my service

public List<FacilityViewModel> GetViewHotel(int HotelID)
        {
            List<Hotel> Hotels = (from d in _HotelRepository.All()
                                  where d.HotelID == HotelID
                             select d).ToList();

            List<FacilityViewModel> facilityViewModel = new List<FacilityViewModel>();

            foreach (Hotel hotel in Hotels)
            {
                facilityViewModel.Add(new FacilityViewModel
                {
                    HotelID = HotelID,
                    HotelName = hotel.HotelName,
                    Address1 = hotel.Address1,
                    Address2 = hotel.Address2,
                    HotelDescription = hotel.HotelDescription,
                    HotelInformation = hotel.HotelInformation,
                });
            }

            return facilityViewModel;
        }

my controller

public ActionResult Index()
        {
            //var x = _hotelService.GetByID(_HotelID);
            List<FacilityViewModel> facilityViewModel = _viewService.GetViewHotel(_HotelID);
            return View(facilityViewModel);
        }

my viewmodel

public class FacilityViewModel
    {
        public int HotelID { get; set; }
        public string HotelName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string HotelDescription { get; set; }
        public string HotelInformation { get; set; }

    }

my view

@model XNet.Repository.Model.FacilityViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<fieldset>
    <legend></legend>

    <div class="display-label">
         @Html.Label("Hotel ID")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.HotelID)
    </div>
    <br />

    <div class="display-label">
         @Html.Label("Hotel Name")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.HotelName)
    </div>
    <br />

    <div class="display-label">
         @Html.Label("Address 1")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Address1)
    </div>
    <br />

     <div class="display-label">
         @Html.Label("Address 2")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Address2)
    </div>
    <br />

     <div class="display-label">
         @Html.Label("Hotel Description")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.HotelDescription)
    </div>
    <br />

     <div class="display-label">
         @Html.Label("Hotel Information")
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.HotelInformation)
    </div>
    <br />
    <br />

</fieldset>

<br />
<input style="width:100px;" type="button" title="EditHotelDetail" value="EditDetails"  onclick="location.href='@Url.Action("Edit", "Hotel", new { HotelID = Model.HotelID})'" />
novian kristianto
  • 751
  • 3
  • 12
  • 34

2 Answers2

2

The error message says it all. Your view has the model typed as just the instance of FacilityViewModel while you are returning a List of FacilityViewModel from the action

In your view:

@model XNet.Repository.Model.FacilityViewModel

In your action:

List<FacilityViewModel> facilityViewModel = _viewService.GetViewHotel(_HotelID);
return View(facilityViewModel); //<-- This is a list.

Either

return View(facilityViewModel.First()) //just an example to show a single instance.

or

Modify the code to handle a list in the view.

PSL
  • 123,204
  • 21
  • 253
  • 243
  • so, how can i solve this??? i try to use @model List, but still error. – novian kristianto May 29 '13 at 03:32
  • @noviankristianto I think you must be getting a different error in that case. – PSL May 29 '13 at 03:33
  • @noviankristianto passing `return View(facilityViewModel.First())` and see what is happening.. – PSL May 29 '13 at 03:35
  • @noviankristianto you are welcome. I am not sure if your req is to show multiple facility views or not. but if yes then you will have to make changes in your view to generate multiple of these partialview dat ato show the list of information rather than just one. – PSL May 29 '13 at 03:48
  • @noviankristianto You will get a lot of example in web, in SO itself. Check this one http://stackoverflow.com/questions/15375800/model-binding-to-a-list-mvc-4. You may probably have this view taking the list and do a forloop of each `facilityViewModel` and generate the records for each of them inside the loop. Just a basic way of doing it. – PSL May 29 '13 at 03:59
1

Your declaring you model for the View as:

@model XNet.Repository.Model.FacilityViewModel

Instead of:

@model List<XNet.Repository.Model.FacilityViewModel>

In your controller's action you are returning a List to the View.

Either modify the model to take a List<FacilityViewMOdel> in the View or return a single FacilityViewModel to the View.

return View(facilityViewModel.FirstOrDefault());
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • i try to return View(new FacilityViewModel());, but i get empty value for my field.. – novian kristianto May 29 '13 at 03:34
  • You need to return an instantiated instance.....Why do you have a list and then you're only returning one to the view anyway? Shouldnt you return the List and show them all? – Gabe May 29 '13 at 03:35