Error using compound view to display search results ASP.NET MVC

I have created a search function in my website where users can choose what type of item they want to search for. As the search can use different models I have made a compound view model to display the data. But when I run the search I receive an error saying 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'.

Any help would be greatful

search function

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);
            }

Search view model

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; }


    }

}

Results page

@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>
    }
}

Answers


Your controller needs to return an object of type BiteWebsite.Models.Search, so for example you will need to return:

if (Searchby == "Restaurant")
{
    ViewData["Type"] = "Restaurant";
    return View(new Search() {
      RestaurantSearch = db.Restaurants.Where(r => r.Name == SearchString).ToList())
    });
}

and basically do the same for the other searches.


Need Your Help

Is there a way to change sectioned tableview to drilldown tableview?

objective-c ios cocoa-touch uitableview datasource

I have a sectioned tableview with a plist wich is an array filled with dictionaries.

Where in this code generates a random number?

c random floating-point

I came across rgba's floating point random number generator:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.