我尝试使用 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);
}
我的视图模型
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})'" />