将数据传递到用户控件 ASP.NET MVC
我有一个用户控件,显示最新公告列表。我几乎 90% 的页面都会使用此用户控件。现在我关心的是如何将数据传递给这个用户控件以获取最新的公告。
我的第一种方法是创建一个基本控制器,并在 Initialise 方法中通过 ViewBag/ViewData 将数据传递给用户控件。我的所有其他控制器均源自此基本控制器。这看起来不错,但我担心的是,对于现有的一些简单解决方案来说,它可能会变得矫枉过正。另外,我需要确保没有控制器会篡改用于我的用户控件的 Viewdata/Viewbag 数据。
请让我知道这是否是正确的继续方式或者是否存在更好的解决方案。
谢谢
I have a user control which shows list of latest announcements. This user control would be used in almost 90% of my pages. Now my concern is how to pass data to this user control for latest announcements.
My first approach is to make a base controller and in Initialise method I pass data to user control via ViewBag/ViewData. All my other controllers derive from this base controller. This looks nice but my concern is that it may become an overkill for some simple solution existing already out there. Also I would need to make sure that no controller ever fiddles with my Viewdata/Viewbag data meant for my usercontrol.
Please let me know if this is correct way of proceeding ahead or there exists some better solution.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您有一个“用户控件”(您应该尝试将它们称为 MVC 中的分部视图),如下所示:
这意味着您的分部视图需要一个 Announcement 对象列表。
现在的问题是 - 你在哪里渲染这个部分视图?
您可以从母版页执行此操作,可以从视图执行此操作,也可以从另一个部分视图执行此操作。
无论哪种方式,渲染部分的代码都需要如下所示:
但是 - 你从哪里获取公告。
假设您有一个 Repository/DAL/helper 方法来获取最新公告 - 我认为您应该拥有您需要从基本 ViewModel 继承的 ViewModel:
那么任何需要呈现最新公告部分的 master/view/partial 都应该绑定到继承自该基本视图模型的 ViewModel。
在master/view/partial不是强类型的情况下(例如动态视图),您可以将其粘贴在ViewData中。但如果您正确组织了视图,则不需要这样做。
Assuming you have a "user control" (you should try to refer to them as partial view's in MVC) that looks like this:
This means your partial view expects a list of Announcement objects.
Now, the question is - where are you rendering this partial view?
You could be doing it from a master page, you could be doing it from a view, or you could be doing it from another partial view.
Either way, the code to render the partial needs to look like this:
But - where do you get the announcements from.
Assuming you have a Repository/DAL/helper method to get the latest announcements - i think you should have the ViewModel's you require inheriting from a base ViewModel:
Then any master/view/partial that needs to render the latest announcements partial should be bound to a ViewModel which inherits from that base view model.
In the cases where the master/view/partial is not strongly-typed (e.g dynamic view), you can stick it in the ViewData. But if you have organized your view's correctly this shouldn't be required.
这就是你所追求的吗? 如何将数据从视图传递到 UserControl在 ASP.NET MVC 中?
Is this the kind of thing you're after? How to pass data from view to UserControl in ASP.NET MVC?
在这种情况下,您应该使用 RenderAction,这样您就不必费心在控制器的每个操作方法中传递所需的数据。
You should use RenderAction in this kind of scenario, so that you do not have bother to pass the required data in each action method of your controllers.
我认为最好的方法是使用@Html.Action。这将允许我调用专用于我的用户控件数据的操作,并且我可以从任何地方调用它。
I think the best way would be to use @Html.Action. This would allow me to call my actions dedicated to my usercontrols data and I can call it from anywhere.