传递List的值

时间:2013-05-29 03:25:42

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我尝试使用viewmodel显示我的数据,但我有这样的错误

传入字典的模型项的类型为'System.Collections.Generic.List`1 [XNet.Repository.Model.FacilityViewModel]',但此字典需要类型为'XNet.Repository的模型项.Model.FacilityViewModel”。

我怎么能解决我的问题? 这是我的代码

我的服务

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;
        }

我的控制器

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

我的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; }

    }

我的观点

@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})'" />

2 个答案:

答案 0 :(得分:2)

错误消息说明了一切。 当您从操作中返回FacilityViewModel列表时,您的视图的模型只是FacilityViewModel的实例类型

在您看来:

@model XNet.Repository.Model.FacilityViewModel

在你的行动中:

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

要么

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

修改代码以处理视图中的列表。

答案 1 :(得分:1)

您声明自己为View建模:

@model XNet.Repository.Model.FacilityViewModel

而不是:

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

在控制器的操作中,您将向列表返回一个列表。

修改模型以在视图中显示List<FacilityViewMOdel>或将单个FacilityViewModel返回到视图。

return View(facilityViewModel.FirstOrDefault());