【发布时间】:2014-03-31 15:59:58
【问题描述】:
我在我的网站中创建了一个搜索功能,用户可以在其中选择他们想要搜索的项目类型。由于搜索可以使用不同的模型,我制作了一个复合视图模型来显示数据。但是当我运行搜索时,我收到一条错误消息The model item passed into the dictionary is of type 'System.Collections.Generic.List1'[BiteWebsite.Models.Restaurant]', but this dictionary requires a model item of type 'BiteWebsite.Models.Search'.
任何帮助都会很棒
搜索功能
public ActionResult Results(string Searchby, string SearchString)
{
if (Searchby == "Restaurant")
{
ViewData["Type"] = "Restaurant";
return View(db.Restaurants.Where(r => r.Name == SearchString).ToList());
}
else
{
if (Searchby == "Cuisine")
{
var type = "Cuisine";
ViewData["Searchtype"] = type;
return View(db.Cuisines.Where(c => c.Name == SearchString).ToList());
}
else
{
if (Searchby == "User")
{
var type = "User";
ViewData["Searchtype"] = type;
return View(db.Users.Where(u => u.FirstName == SearchString).ToList());
}
}
}
return View(SearchString);
}
搜索视图模型
namespace BiteWebsite.Models
{
public class Search
{
public IEnumerable<BiteWebsite.Models.ApplicationUser> UserSearch { get; set; }
public IEnumerable<BiteWebsite.Models.Restaurant> RestaurantSearch { get; set; }
public IEnumerable<BiteWebsite.Models.Cuisine> CuisineSearch { get; set; }
[Display (Name ="Name")]
public string Name { get; set; }
[Display(Name = "Cuisine")]
public string Cuisine { get; set; }
[Display(Name = "Street Number")]
public string StreetNumber { get; set; }
[Display(Name = "Street Name")]
public string StreetName { get; set; }
[Display(Name = "County")]
public string County { get; set; }
[Display(Name = "PostCode")]
public string PostCode { get; set; }
[Display(Name = "Contact Number")]
public string Contactnumber { get; set; }
[Display(Name = "Website")]
public string Website { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
}
}
结果页面
@model BiteWebsite.Models.Search
@{
ViewBag.Title = "Results";
}
@if (ViewData["Type"] == "Restaurant")
{
<h2>Results</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Cuisine)
</th>
<th>
@Html.DisplayNameFor(model => model.StreetNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.StreetName)
</th>
<th>
@Html.DisplayNameFor(model => model.County)
</th>
<th>
@Html.DisplayNameFor(model => model.PostCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Contactnumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Website)
</th>
<th></th>
</tr>
@foreach (var RestaurantSearch in Model.RestaurantSearch) {
<tr>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.Name)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.Cuisine)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.StreetNumber)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.StreetName)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.County)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.PostCode)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.Contactnumber)
</td>
<td>
@Html.DisplayFor(modelItem => RestaurantSearch.Website)
</td>
<td>
@Html.ActionLink("Details", "Details", "Restaurant", new { id = RestaurantSearch.id }, null) |
</td>
</tr>
}
</table>
}
else {
if(ViewData["Type"] == "Cuisine")
{<h2>Results</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var CuisineSearch in Model.CuisineSearch) {
<tr>
<td>
@Html.DisplayFor(modelItem => CuisineSearch.Name)
</td>
<td>
@Html.DisplayFor(modelItem => CuisineSearch.Description)
</td>
<td>
@Html.ActionLink("Details", "Details", "Restaurant", new { id = CuisineSearch.Id }, null) |
</td>
</tr>
}
</table>
}
}
【问题讨论】:
标签: c# html search model-view-controller asp.net-mvc-5