views:

142

answers:

1

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?

A: 

I've always solved this by having a Partial View which loops over an IEnumerable<T> and calls Html.DisplayFor() on each item, but then I didn't even know you could call Html.DisplayFor() on an IEnumerable<T> and have it automatically render each templated element until you said so in your question. Thanks for that, by the way! :)

In any case, I think your best bet is to simply return a PartialView() which accepts a collection of Employees and renders them one at a time calls Html.DisplayFor(). It's not as elegant as returning an HtmlHelper from your controller, but at least it's simple enough to implement.

Nathan Taylor
It looking like that be what I have to do. Ideally I want to avoid creating partial views with just 1 line of code. I've been looking at make creating a new DisplayFor action result, but again, I haven't been able to get this to work. Anyway, thanks for your input, and I'm glad I was able to help you :)
Darragh