如何使用DisplayFor而不是ASP中的控制器呈现对象列表。净MVC吗?

时间:2021-07-31 03:43:25

Here's the scenaio, I have an Employee object and a Company object which has a list of employees.

这是scenaio,我有一个员工对象和一个有员工列表的公司对象。

I have Company.aspx which inherits from ViewPage<Company>.

我有公司。从ViewPage 继承的aspx。

In Company.aspx I call

在公司。aspx我打电话

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

I have an Employee.ascx partial view which inherits from ViewUserControl<Employee> in my DisplayTemplates folder.

我有一个员工。ascx部分视图,它从ViewUserControl 继承到我的DisplayTemplates文件夹中。

Everything works fine and Company.aspx renders the Employee.ascx partial for each employee.

一切都很顺利,陪伴着你。aspx呈现员工。ascx为每个员工提供部分服务。

Now I have two additional methods on my controller called GetEmployees and GetEmployee(Id).

现在,我的控制器上有两个额外的方法叫做GetEmployees和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).

在GetEmployee(Id)操作中,我希望返回用于显示这个员工的标记,在GetEmployees()中,我希望显示用于显示所有员工的标记(这两个操作方法将通过AJAX调用)。

In the GetEmployee action I call

在GetEmployee操作中调用

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?

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

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.

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

Any ideas? Have i missed something obvious?

什么好主意吗?我漏掉了什么明显的东西吗?

1 个解决方案

#1


2  

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! :)

我总是通过一个局部视图来解决这个问题,它在IEnumerable 上循环,并在每个条目上调用Html.DisplayFor(),但是我甚至不知道在IEnumerable 上调用Html.DisplayFor()并让它自动呈现每个模板元素,直到你在你的问题中这么说。顺便说一下,谢谢你!:)

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.

无论如何,我认为您最好的办法是简单地返回一个PartialView(),它接受一个雇员集合,并在一次调用Html.DisplayFor()时呈现它们。它并不像从控制器返回HtmlHelper那样优雅,但至少它很简单,可以实现。

#1


2  

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! :)

我总是通过一个局部视图来解决这个问题,它在IEnumerable 上循环,并在每个条目上调用Html.DisplayFor(),但是我甚至不知道在IEnumerable 上调用Html.DisplayFor()并让它自动呈现每个模板元素,直到你在你的问题中这么说。顺便说一下,谢谢你!:)

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.

无论如何,我认为您最好的办法是简单地返回一个PartialView(),它接受一个雇员集合,并在一次调用Html.DisplayFor()时呈现它们。它并不像从控制器返回HtmlHelper那样优雅,但至少它很简单,可以实现。