0

我有一个视图模型类,它有一个名为 Title 的属性,我想在 Razor 的索引视图上呈现它

班级

public class vmAppointment
{
    public Appointment Appointment { get; private set; }
    public IEnumerable<SelectListItem> EmployeeList { get; set; }
    public string Title { get; set; }
}

索引视图

@model IEnumerable<JJ.ViewModels.vmAppointment>

<h2>@Model.Title</h2>  WRONG!  how can i reference my property here?

如何在视图中呈现属性值?

4

1 回答 1

4

您的视图是强类型的集合vmAppointment,而不是单个vmAppointment. 所以为了引用Title你将不得不遍历这个集合的元素。例如:

@foreach (var appointment in Model)
{
    <div>Appointment title: @appointment.Title</div>
}

或者,如果您希望将视图强类型化为单个vmAppointment而不是集合,请让控制器操作返回此类的单个实例:

vmAppointment model = ...
return View(model);

然后将您的视图强输入到单个约会中,您将能够直接引用该Title属性:

@model JJ.ViewModels.vmAppointment
<h2>@Html.DisplyFor(x => x.Title)</h2>
于 2013-07-02T20:17:19.090 回答