how do I display the text value(CountryName) from the selected id (CountryId) in a SelectList? The SelectList is contained in a view model sent to Details view template. The field is currently displaying CountryID. I want CountryName.
view
<div class="display-field">@Model.Dinner.CountryID</div>
controller
public ActionResult Details(int id)
{
Dinner dinner = dinnerRepository.GetDinner(id);
DinnerFormViewModel model = new DinnerFormViewModel(dinner);
if (dinner == null)
return View("NotFound");
else
return View("Details", model);
}
viewmodel
public class DinnerFormViewModel
{
public Dinner Dinner { get; private set; }
public SelectList CountriesList { get; private set; }
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
var items = new List<Country>() {
new Country() {
CountryID = 1,
CountryName = "England"
},
new Country() {
CountryID = 2,
CountryName = "Ireland"
},
new Country() {
CountryID = 3,
CountryName = "Scotland"
},
new Country() {
CountryID = 3,
CountryName = "Wales"
}
};
CountriesList = new SelectList(items, "CountryID", "CountryName", 2);
}
}
Again this is just to display the CountryName value in a label. Dont want to edit it or anything. LINQ expression?