2013-07-19 25 views
1

我有一個BookingView類型號:MVC3顯示錶中的所有記錄上查看

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 complie時間誤差在return語句。如果我將返回類型更改爲Ticket,那麼我得到運行時錯誤,因爲在查看ViewAllBookings期望IEnumerable類型爲BookingView的記錄列表。

請爲此提供解決方案。我真的很困惑如何處理這個問題。

感謝

回答

2

它看起來像你的設置需要的實體類Ticket轉換成視圖模型BookingView。我想你需要的東西是這樣的:

return a.AsEnumerable().Select(ticket => new BookingView 
    { 
     Attraction = ticket.SomeProperty, 
     // etc 
    }) 
.ToList(); 
+0

謝謝。有效! – Azeem

1

你必須寫一個映射票到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; 
} 
1

我不知道,如果你的Tickets表實際上具有相同在你的代碼BookingView班列,但如果theere的表和類之間的映射,你的解決辦法是:

var a = from o in dre.Tickets 
     select new BookingView { Attraction= o.Attraction, 
            Date=o.Date, 
            Username = o.UserName } ; 
return a.ToList(); 
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; 
    } 
} 

在預訂控制器,你可以注入一個實例的IBookingManagerdependency injection frameworkAutoFac

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 
} 

我希望這有助於。

相關問題