I have a model called ComplaintModel:
public class ComplaintModel
{
private Data.MTAEntities db = new MTAEntities();
public IEnumerable<Complaint> Complaints { get; set; }
public void GetAllComplaints()
{
var complaints = (from c in db.tbComplaints
orderby c.CreateDateTime descending
select
new Complaint()
{
Comment = c.Complaint,
ComplaintID = c.ComplaintID,
CreateDateTime = c.CreateDateTime
});
Complaints = complaints;
}
On m开发者_开发问答y index page I have the following code:
<% Html.DisplayFor(m => Model.Complaints, "Complaint"); %>
I am pretty much passing an IEnumerable object to my Display Template. In my display template:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MTA.Models.Entities.Complaint>>" %>
<%@ Import Namespace="MTA.Models.Entities" %>
<% foreach (Complaint complaint in Model)
{
%>
<div id="complaint">
<%:complaint.Comment%>
</div>
<%
}%>
I definitely have one Complaint class in my collection, for some reason it is not displaying. Am I missing something? I also don't get any errors which makes it harder to figure out what I am doing wrong. Only thing I can think of its being lost before it gets to my Display Template somehow?
Thank you!
EDITS:
The exact path to the user control is: ~/Views/Home/DisplayTemplates/Complaint.ascx
The controller method call:
public ActionResult Index()
{
var model = new MTA.Models.ComplaintModel();
model.GetAllComplaints();
ViewData["model"] = model;
return View(model);
}
How about this ? (use ComplaintModel instead)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MTA.Models.Entities.ComplaintModel>" %>
<%@ Import Namespace="MTA.Models.Entities" %>
<% foreach (Complaint complaint in Model.Complaints)
{
%>
<div id="complaint">
<%:complaint.Comment%>
</div>
<%}%>
精彩评论