Featured post
Pass data to User Control ASP.NET MVC -
i have user control shows list of latest announcements. user control used in 90% of pages. concern how pass data user control latest announcements.
my first approach make base controller , in initialise method pass data user control via viewbag/viewdata. other controllers derive base controller. looks nice concern may become overkill simple solution existing out there. need make sure no controller ever fiddles viewdata/viewbag data meant usercontrol.
please let me know if correct way of proceeding ahead or there exists better solution.
thanks
assuming have "user control" (you should try refer them partial view's in mvc) looks this:
<%@ control language="c#" inherits="system.web.mvc.viewusercontrol<ienumerable<announcement>>" %>
this means partial view expects list of announcement objects.
now, question - where rendering partial view?
you doing master page, doing view, or doing partial view.
either way, code render partial needs this:
<% html.renderpartial("latestannouncements", announcements) %>
but - announcements from.
assuming have repository/dal/helper method latest announcements - think should have viewmodel's require inheriting base viewmodel:
public class announcementviewmodelbase { protected ienumerable<announcement> getannouncements() { // call dal } }
then master/view/partial needs render latest announcements partial should bound viewmodel inherits base view model.
in cases master/view/partial not strongly-typed (e.g dynamic view), can stick in viewdata. if have organized view's correctly shouldn't required.
- Get link
- X
- Other Apps
Comments
Post a Comment