如何使用 DisplayFor 但从 ASP.NET MVC 中的控制器渲染对象列表?

发布于 2024-09-01 13:13:25 字数 1196 浏览 7 评论 0 原文

这是场景,我有一个 Employee 对象和一个包含员工列表的 Company 对象。

我有 Company.aspx ,它继承自 ViewPage

在 Company.aspx 中,我调用

Html.DisplayFor(m => m.Employees).

我有一个继承自 ViewUserControl> 的 Employee.ascx 部分视图。在我的 DisplayTemplates 文件夹中。

一切正常,Company.aspx 为每个员工呈现 Employee.ascx 部分。

现在,我的控制器上有两个附加方法,名为 GetEmployeesGetEmployee(Id)

GetEmployee(Id) 操作中,我想返回标记以显示该员工,在 GetEmployees() 操作中,我想呈现标记以显示所有员工 (这两个操作方法将通过 AJAX 调用)。

在 GetEmployee 操作中,我调用

return PartialView("DisplayTemplates\Employee", employee)

This 有效,尽管我更喜欢类似的方法

return PartialViewFor(employee)

来按约定确定视图名称。

Anwyay,我的问题是我应该如何实现 GetEmployees() 操作?

我不想创造更多的观点,因为坦率地说,我不明白为什么我必须这样做。

我已经尝试了以下方法,但失败了:)

return Content(New HtmlHelper<IList<Of DebtDto>>(null, null).DisplayFor(m => debts));

但是,如果我可以在控制器中创建 HtmlHelper 对象的实例,我想我可以让它工作,但感觉不对。

有什么想法吗?我错过了一些明显的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-09-08 13:13:25

我总是通过使用一个局部视图来解决这个问题,该视图循环遍历 IEnumerable 并在每个项目上调用 Html.DisplayFor() ,但后来我没有甚至知道您可以在 IEnumerable 上调用 Html.DisplayFor() 并让它自动渲染每个模板化元素,直到您在问题中这么说。顺便说一句,谢谢你! :)

无论如何,我认为最好的选择是简单地返回一个 PartialView() ,它接受一组员工并 一次渲染一个 调用 Html.DisplayFor()。它不像从控制器返回 HtmlHelper 那样优雅,但至少它实现起来足够简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文