Reputation: 9921
I have a view model class that has an attribute called Title that I would like to render on the Index view in Razor
CLASS
public class vmAppointment
{
public Appointment Appointment { get; private set; }
public IEnumerable<SelectListItem> EmployeeList { get; set; }
public string Title { get; set; }
}
INDEX VIEW
@model IEnumerable<JJ.ViewModels.vmAppointment>
<h2>@Model.Title</h2> WRONG! how can i reference my property here?
how can I render the property value in the view?
Upvotes: 0
Views: 6737
Reputation: 1039458
Your view is strongly typed to a collection of vmAppointment
, not a single vmAppointment
. So in order to reference the Title
you're gonna have to loop through the elements of this collection. For example:
@foreach (var appointment in Model)
{
<div>Appointment title: @appointment.Title</div>
}
Alternatively if you want your view to be strongly typed to a single vmAppointment
instead of a collection have your controller action return a single instance of this class:
vmAppointment model = ...
return View(model);
and then make your view strongly typed to a single appointment and you will be able to directly reference the Title
property:
@model JJ.ViewModels.vmAppointment
<h2>@Html.DisplyFor(x => x.Title)</h2>
Upvotes: 4