样板房:
public class Room
{
public int Id { get; set; }
public string NumberRoom { get; set; }
public double CostPerNight { get; set; }
public virtual Category Category { get; set; }
}
我的模型代码
public class RoomModel
{
public IList<Room> Rooms { get; set; }
}
我的剃刀代码:
@model hotel.Models.RoomModel
@using (Html.BeginForm("ComfortLevelView", "Category"))
{
for (int i = 0; i < Model.Rooms.Count(); i++)
{
<table class="simple-little-table" cellspacing='0'>
<tr>
<td>@Html.DisplayFor(m => Model.Rooms[i].NumberRoom) </td>
<td>@Html.DisplayFor(m => Model.Rooms[i].Categoryid)</td>
<td>@Html.DisplayFor(m => Model.Rooms[i].NumberOfSeats) </td>
<td>
@{ var result = Model.Rooms[i].CostPerNight * numberNights; }
<p>@ViewBag.NumberNights ночей</p>:@result
</td>
<td>
<input type="submit" id="submit" value="Booking" />
</td>
</tr>
</table>
</div>
}
}
控制器:
public ActionResult ComfortLevelView(int NumberNights, int CategoryId, int NumberPeoples ,DateTime SelectedDate)
{
IRoomService roomService = new RoomService();;
return View(roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples));
}
[HttpPost]
public ActionResult ComfortLevelView(RoomModel model)
{
//
}
传递到字典中的模型项的类型是&#39; System.Data.Entity.Infrastructure.DbQuery`1 [Hotel.BusinessObject.Room]&#39;,但此字典需要类型为&的模型项#39; hotel.Models.RoomModel&#39;
答案 0 :(得分:1)
错误消息是不言自明的。你在视图中有这个
@model hotel.Models.RoomModel
但是由于控制器中的这行代码,您将System.Data.Entity.Infrastructure.DbQuery<Hotel.BusinessObject.Room>
的实例传递给了您的视图
return View(roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples));
您需要传递RoomModel
而不是System.Data.Entity.Infrastructure.DbQuery<Hotel.BusinessObject.Room>
的实例。我建议将控制器代码更改为
public ActionResult ComfortLevelView(int NumberNights, int CategoryId, int NumberPeoples, DateTime SelectedDate)
{
IRoomService roomService = new RoomService();
var rooms = roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples);
RoomModel model = new RoomModel();
model.Rooms = rooms.ToList();
return View(model);
}