尝试让视图使用 MVC 中的视图模型



我有一个从数据类中检索数据的 ViewModel。

然后,我更改了控制器索引方法,以便能够使用类似于此示例的视图模型:

namespace WebSite.Models.PassengerViewModels
{
public class PassengerViewModel
{
[Display(Name = "Passenger ID ")]
public int Id { get; set; }
[Display(Name = "Surname ")]
[Required]
public string Surname { get; set; }
[Display(Name = "First Name ")]
[Required]
public string FirstName { get; set; }
[Display(Name = "E-mail Address ")]
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
}
}

然后在乘客控制器中,索引我更改为此。 我尝试为姓氏和名字添加一个过滤系统:

public async Task<IActionResult> Index(string filter)
{
var passengerContext = from e in _context.Passengers
.Select(a => new PassengerViewModel
{
Id = a.Id,
FirstName = a.FirstName,
Surname = a.Surname,
Email = a.Email,
})
select e;

if (!String.IsNullOrEmpty(filter))
{
passengerContext = passengerContext.Where(e => e.FirstName.Contains(filter)).Where(e => e.Surname.Contains(filter));
}
return View(await _context.Passengers.ToListAsync());
}

然后在"查看索引"页中,代码如下所示:

@model IEnumerable<WebSite.Models.PassengerViewModels.PassengerViewModel>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="PassengersController" asp-action="Index">
<p>
Event: <input type="search" name="filter">
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a>   |
<a asp-action="Detail" asp-route-id="@item.Id">Detail</a>
</td>
</tr>
}
</tbody>
</table>

然后,当我尝试进入页面时,我收到此错误,我不确定它确切地告诉我什么:

InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为 'System.Collections.Generic.List1[WebSite.Data.Passenger]', but this ViewDataDictionary instance requires a model item of type System.Collections.Generic.IEnumerable1[Website.Models.PassengerViewModels.PassengerViewModel]"。

您在return中的问题,您应该将return更新为此

return View(await _context.Passengers.ToListAsync());
to
return View(passengerContext.ToList());

此行直接从原始上下文传递数据

return View(await _context.Passengers.ToListAsync());

当你真正想要的是

return View(await passengerContext.ToListAsync());

应相应地重构操作

public async Task<IActionResult> Index(string filter) {
var passengers = _context.Passengers
.Select(a => new PassengerViewModel {
Id = a.Id,
FirstName = a.FirstName,
Surname = a.Surname,
Email = a.Email,
});
if (!String.IsNullOrEmpty(filter)) {
passengers = passengers.Where(e => 
e.FirstName.Contains(filter) || 
e.Surname.Contains(filter)
);
}
return View(await passengers.ToListAsync());
}

相关内容

  • 没有找到相关文章

最新更新