我正在为学校建设一个项目。我快到了,但是有一个我无法解决的错误。 我正在预约asp.net网站。 该错误表明我将一个列表传递到保留的索引页,并且期望IEnumerable。 我已经尝试过将期望的列表,但这也给出了错误。
我已经看到其他解决此问题的方法,但是我无法使其正常工作。
这是我最初的索引方法。
// GET: Reservations
public ActionResult Index()
{
var reservations = db.Reservations.Include(r => r.Bike).Include(r => r.Customer).Include(r => r.DropoffStore).Include(r => r.PickupStore);
return View(reservations.ToList());
}
这是我从其他人那里获得的新的Controller索引方法,但是我不知道为什么我不会工作:
// GET: Reservations
public ActionResult Index()
{
var reservations = db.Reservations.Include(r => r.Bike).Include(r => r.Customer).Include(r => r.DropoffStore).Include(r => r.PickupStore);
IEnumerable<ReservationViewModel> reservationViewModels = new List<ReservationViewModel>();
foreach (var reservation in reservations)
{
var reservationViewModel = new ReservationViewModel
{
};
reservationViewModels.ToList().Add(reservationViewModel);
}
return View(reservationViewModels);
}
这是我的索引页:
@model IEnumerable<ASP.NET_Framwork_for_real_bitch.ViewModels.ReservationViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Reservation.Customer.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.Customer.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.Customer.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.Customer.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.DropoffStore_Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.PickupStore_Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.StartDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Reservation.EndDate)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Reservation.Customer.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.Customer.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.Customer.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.Customer.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.DropoffStore.StoreName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.PickupStore.StoreName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.StartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Reservation.EndDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Reservation.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Reservation.Id })
</td>
</tr>
}
</table>
这是确切的错误:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[ASP.NET_Framwork_for_real_bitch.Models.Reservation]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ASP.NET_Framwork_for_real_bitch.ViewModels.ReservationViewModel]'.
我的整个ReservationViewModel:
using ASP.NET_Framwork_for_real_bitch.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ASP.NET_Framwork_for_real_bitch.ViewModels
{
public class ReservationViewModel
{
private BikeShoppaModel db = new BikeShoppaModel();
public Reservation Reservation { get; set; }
public SelectList DropoffStore_Id { get; private set; }
public SelectList PickupStore_Id { get; private set; }
public SelectList Bikes { get; private set; }
public SelectList CustomerGender { get; set; }
public int TotalDays { get; set; }
public double TotalPrice { get; set; }
public ReservationViewModel()
{
DropoffStore_Id = new SelectList(db.Stores, "Id", "StoreName");
PickupStore_Id = new SelectList(db.Stores, "Id", "StoreName");
Bikes = new SelectList(db.Bikes, "Id", "Brand");
CustomerGender = new SelectList(db.Customers, "Id", "Gender");
}
public ReservationViewModel(int id) : this()
{
Reservation = db.Reservations.Find(id);
TotalDays = (Reservation.EndDate.Date - Reservation.StartDate.Date).Days + 1;
}
public void Save()
{
if(Reservation.Id > 0)
{
db.Entry(Reservation).State = EntityState.Modified;
}
else
{
db.Reservations.Add(Reservation);
}
db.SaveChanges();
}
}
}
我的预定模式:
public class Reservation
{
public int Id { get; set; }
[ForeignKey("Customer")]
[Display(Name = "Customer")]
public int Customer_Id { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Bike")]
public int Bike_Id { get; set; }
public Bike Bike { get; set; }
[DataType(DataType.Date)]
[Column(TypeName = "Date")]
[Display(Name = "Start date")]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[Column(TypeName = "Date")]
[Display(Name = "End date")]
public DateTime EndDate { get; set; }
[ForeignKey("PickupStore")]
[Display(Name = "Pickup store")]
public int PickupStore_Id { get; set; }
public Store PickupStore { get; set; }
[ForeignKey("DropoffStore")]
[Display(Name = "Dropoff store")]
public int DropoffStore_Id { get; set; }
public Store DropoffStore { get; set; }
}
答案 0 :(得分:0)
您将在Controller索引方法中返回保留列表。您的视图需要一个IEnumerable ReservationViewModels。因此,您的Controller可以生产苹果,但视图需要橙色。 因此,您必须将苹果转换成橘子才能完成这项工作-作为软件开发人员,完成这项工作的众多优势之一:-) 尝试这样的事情:
var reservations = db.Reservations.Include(r => r.Bike).Include(r => r.Customer).Include(r => r.DropoffStore).Include(r => r.PickupStore);
IEnumerable<ReservationViewModel> reservationViewModels = new List<ReservationViewModel>();
foreach (var reservation in reservations)
{
var reservationViewModel = new ReservationViewModel
{
OneProperty = reservation.AnotherProperty
//... other properties you need to convert ...
}
reservationViewModels.Add(reservationViewModel);
}
return View(reservationViewModels);
因此,您必须遍历您的苹果(Reservation类型的对象)并为每个苹果产生一个橙色(ReservationViewModel类型的对象)。最后,您将那组橘子(IEnuerable of ReservationViewModel =传递给视图并完成)。
答案 1 :(得分:0)
请在下面替换“控制器索引”操作方法,并发表您的评论:
// GET: Reservations
public ActionResult Index()
{
var reservations = db.Reservations.Include(r => r.Bike).Include(r => r.Customer).Include(r => r.DropoffStore).Include(r => r.PickupStore);
var reservationViewModels = new List<ReservationViewModel>();
foreach (var reservation in reservations)
{
var reservationViewModel = new ReservationViewModel
{
};
reservationViewModels.Add(reservationViewModel);
}
/*
// In short you can populate your viewmodels and return as below:
return (reservations.Select(r => new ReservationViewModel{
}).AsEnumerable());
*/
return View(reservationViewModels.AsEnumerable());
}
请注意:您基本上将实体模型和视图模型混合在一起。它需要大量的清洁和/或重构。