这是场景,我有一个 Employee 对象和一个包含员工列表的 Company 对象。
我有 Company.aspx
,它继承自 ViewPage
。
在 Company.aspx 中,我调用
Html.DisplayFor(m => m.Employees).
我有一个继承自 ViewUserControl> 的 Employee.ascx
部分视图。在我的 DisplayTemplates 文件夹中。
一切正常,Company.aspx 为每个员工呈现 Employee.ascx 部分。
现在,我的控制器上有两个附加方法,名为 GetEmployees
和 GetEmployee(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?
发布评论
评论(1)
我总是通过使用一个局部视图来解决这个问题,该视图循环遍历
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 callsHtml.DisplayFor()
on each item, but then I didn't even know you could callHtml.DisplayFor()
on anIEnumerable<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 andrenders them one at a timecallsHtml.DisplayFor()
. It's not as elegant as returning an HtmlHelper from your controller, but at least it's simple enough to implement.