有人可以纠正我对 ViewDataDictionary 工作原理的理解吗?
有人可以纠正我对这里发生的事情的理解吗?
假设如下:
<% foreach (var company in Model.Companies) { %>
<% Html.RenderPartial("FundList", Model, new ViewDataDictionary(company)); %>
<% } %>
我的印象是,这将渲染一个名为 FundList.ascx
的部分视图,同时传递一个 Model
对象(其中包含大量内容),并且也是一个company对象,其中包含特定于公司的数据。
但是,当我检查 FundList
中可用的数据时,我只能看到对原始 Model
对象的引用。我在任何地方都没有看到company
。这应该在 ViewData
中可用吗?
对我来说,如何在 FundData
部分中获取 company
对象最好?
Can somebody correct my understanding of what's going on here?
Suppose the following:
<% foreach (var company in Model.Companies) { %>
<% Html.RenderPartial("FundList", Model, new ViewDataDictionary(company)); %>
<% } %>
I was under the impression that this will render a Partial View called FundList.ascx
, while passing a Model
object (which contains a load of stuff), and also a company
object, which contains data specific to a company.
However, when I inspect the data available to me in FundList
, I can only see references to the original Model
object. I don't see company
anywhere. Should this be available in ViewData
?
How would it be best for me to get a company
object in the FundData
partial?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你尝试过吗...
Did you try...
将 foreach 移至局部视图中,从而不再需要此视图数据怎么样?对我来说看起来是最干净的解决方案。
如果没有,您确定您确实需要“模型”吗?难道公司不应该是你的榜样,而其他的东西不应该是片面的吗?
否则,请选择包含所需所有属性的视图模型。
How about just moving the foreach into your partial view, removing the need for this viewdata? Looks like the cleanest solution to me.
If not, are you sure you actually need the "Model"? Isn't the company supposed to be your model while the other stuff should not be concerning the partial view?
Otherwise, go for a viewmodel containing all properties needed.
您的部分应该继承自类型
ViewPage
,然后您可以直接将当前公司作为视图模型传递。托管页面的 ViewDataDictionary 将自动逾越到部分。
your partial should inherit from type
ViewPage<Company>
and then you can pass the current company direct as viewmodel.The ViewDataDictionary of the hosting page will passover to the partial automatically.