c# - 我必须为我的控制器索引方法提供哪些属性?
问题描述
我正在为学校建立一个项目。我快到了,但有一个错误我无法修复。我正在做一个自行车预订 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());
}
这是我从其他人那里获得的新控制器索引方法,但我不知道为什么我不会工作:
// 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
{
(PROPERTY)
};
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]'.
我的整个预订视图模型:
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; }
}
解决方案
推荐阅读
- amazon-web-services - 运行 Amplify Auth Update 后运行 Amplify Push 时出错
- bash - .bashrc 中的函数 ping+() 不起作用
- ruby - 测试进入 rspec(用户输入)
- angular - 在 Angular 中使用字体加载器
- .net - 如何将背景图像与 Windows 窗体中的按钮对齐(Visual Studio)
- google-apps-script - 如何在 Google docs 上按 Enter 键运行我的功能
- tomcat - tomcat 不使用 JDBCRealm,而是坚持使用 UserDatabaseRealm
- spring - spock,在 spring bean 中模拟方法响应
- sql - 比较两个表中的主要/别名组
- google-apps-script - Google Apps Script Web Apps 中出现新的“currentURL is not defined”错误