1

我在模型中有一个 BookingView 类:

public class BookingView
{
    [Required]
    [Display(Name = "Attraction")]
    public int Attraction { get; set; }

    [Required]
    [Display(Name = "Date")]
    public string Date { get; set; }

    [Required]
    [Display(Name = "Username")]
    public string Username { get; set; }
}

数据库中针对此模型的表是Tickets.

我需要在模型中的另一个类中编写一个函数,BookingManager以获取所有票证记录。

public IEnumerable<BookingView> GetAllBookings()
{
    var a = from o in dre.Tickets select o;
    return a.ToList();
}

我想在名为的视图中显示这些记录ViewAllBookings

@model IEnumerable<VirtualTickets.Models.ViewModel.BookingView>

@{
  ViewBag.Title = "ViewAllBookings";
}

<h2>ViewAllBookings</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th>
        Attraction
    </th>
    <th>
        Date
    </th>
    <th>
        Username
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Attraction)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Username)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
    </td>
</tr>
}

</table>

该函数GetAllBookings在返回语句的函数中给出了编译时间错误。如果我将返回类型更改为,Ticket那么我会按预期得到运行时错误,因为在视图中ViewAllBookings它期望 IEnumerable List of records have type BookingView

请提供这种情况的解决方案。我真的很困惑如何处理这个问题。

谢谢

4

4 回答 4

2

看起来您的设置需要将实体类Ticket转换为视图模型BookingView。我想你需要这样的东西:

return a.AsEnumerable().Select(ticket => new BookingView 
    {
        Attraction = ticket.SomeProperty,
        // etc
    })
.ToList();
于 2013-07-19T19:47:13.553 回答
1

您必须编写将 Ticket 映射到 BookingView 的方法并像这样使用它:

public IEnumerable<BookingView> GetAllBookings()
{
    var a = from o in dre.Tickets select o;
    return a.AsEnumerable().Select(Map).ToList();
}

private BookingView Map(Ticket ticket)
{
    var bookingView = new BookingView();
    //mapping code goes here
    return bookingView;
}
于 2013-07-19T19:49:45.897 回答
1

我不确定您的Tickets表是否实际上与代码中的类具有相同的列BookingView,但如果表和您的类之间存在映射,您的解决方案将是:

var a = from o in dre.Tickets 
         select new BookingView  { Attraction= o.Attraction, 
                                   Date=o.Date, 
                                   Username = o.UserName } ;
return a.ToList();
于 2013-07-19T19:54:55.433 回答
0

是的,很明显您会收到此错误,因为您正在返回一个Ticket对象并且您的视图需要一个BookingView对象列表。2 不匹配,即它们不一样。

BookingManager应该将票退还给控制器。这是我将如何做到的另一种方法。

您可以将视图模型更改为如下所示:

public class BookingViewModel
{
     public List<Ticket> Tickets { get; set; }
}

BookingManager可以包含以下方法来退票:

public class BookingManager : IBookingManager
{
     // References to your database context

     public IEnumerable<Ticket> GetAllTickets()
     {
          return databaseContext.Tickets;
     }
}

在您的预订控制器中,您可以注入IBookingManager一个dependency injection framework类似的实例AutoFac

public class BookingController : Controller
{
     private readonly IBookingManager bookingManager;

     public BookingController(IBookingManager bookingManager)
     {
          this.bookingManager = bookingManager;
     }

     public ActionResult Index()
     {
          BookingViewModel viewModel = new BookingViewModel
          {
               Tickets = bookingManager.GetAllTickets().ToList()
          };

          return View(viewModel);
     }
}

然后在你看来:

@model YourProject.ViewModels.Tickets.BookingViewModel

@foreach (var ticket in Model.Tickets)
{
     // Display ticket details here
}

我希望这有帮助。

于 2013-07-22T10:47:37.150 回答