I’m trying to add a dropdown list to my Hotel view – index.cshtml. The hotel controller for this view gets the Hotel object:
private HotelEntities db = new HotelEntities();
public ViewResult Index()
{
var hotels = db.Hotels.Include("Address");
return View(hotels.ToList());
}
I want to display a list of hotels in this view. But before showing this list, I want the user to be able to search which country they want to view hotels for.
The dropdownlist is a list of countries in my database. I have an object for it in my entity model – HotelEntities.
I have created a partial country view in the shared folder with the dropdownlist and a country controller for it that gets the list of countries:
public class CountryController : Controller
{
private HotelEntities db = new HotelEntities();
//
// GET: /Country/
public ActionResult Index()
{
var country = db.Countries.ToList();
return View(country.ToList());
}
}
In the partial view I have :
@model IEnumerable <MvcApp20Aug.Models.Country>
@foreach (var item in Model)
{
@Html.DropDownListFor(model => item.CountryId, new SelectList(item.CountryIso, item.CountryName));
}
and finally i added the partial view into my hotels index.cshtml:
@model IEnumerable<MvcHotelApp.Models.Hotel>
@{
ViewBag.Title = "Index";
}
<h2>Index </h2>
@{Html.RenderPartial("Country");}
<p>
I get the error : The model item passed into the dictionary is of type ‘System.Collections.Generic.List`1[MvcHotelApp.Models.Hotel]’, but this dictionary requires a model item of type ‘MvcHotelApp.Models.Country’.
So I don’t understand how to use a different table in my partial view to my main view.
Can anyone explain why not and how i should approach this.
call it like ,
and change you
countrypartialview like this,