How can I render a list of objects using DisplayFor but from the controller in ASP.NET MVC?
Posted
by Darragh
on Stack Overflow
See other posts from Stack Overflow
or by Darragh
Published on 2010-05-13T10:57:10Z
Indexed on
2010/05/13
11:04 UTC
Read the original article
Hit count: 457
Here's the scenaio, I have an Employee object and a Company object which has a list of employees.
I have Company.aspx
which inherits from ViewPage<Company>
.
In Company.aspx I call
Html.DisplayFor(m => m.Employees).
I have an Employee.ascx
partial view which inherits from ViewUserControl<Employee
> in my DisplayTemplates folder.
Everything works fine and Company.aspx
renders the Employee.ascx
partial for each employee.
Now I have two additional methods on my controller called GetEmployees
and GetEmployee(Id)
.
In the GetEmployee(Id)
action I want to return the markup to display this one employee, and in GetEmployees()
I want to render the markup to display all the employees (these two action methods will be called via AJAX).
In the GetEmployee action I call
return PartialView("DisplayTemplates\Employee", employee)
This works, although I'd prefer something like
return PartialViewFor(employee)
which would determine the view name by convention.
Anwyay, my question is how should I implement the GetEmployees()
action?
I don't want to create any more views, because frankly, I don't see why I should have to.
I've tried the following which fails miserably :)
return Content(New HtmlHelper<IList<Of DebtDto>>(null, null).DisplayFor(m => debts));
However if I could create an instance of an HtmlHelper object in my controller, I suppose I could get it to work, but it feels wrong.
Any ideas? Have i missed something obvious?
© Stack Overflow or respective owner