Due to the confusion between all the information that there is about mvc from all preview releases and one official release, I am very confused about how to deal with viewusercontrols. So, once and for all, tell me how to implement this example:
I have a list of upcoming events that should be displayed on several pages of my site. So I put a new ViewUserControl (ListEvents.ascx) inside my Views \ Shared folder.
I am requesting this ListEvents.ascx to display in my Home / Index view as follows:
<p> Here a list of events: <% Html.RenderPartial("ListEvents");%> </p>
How do I go about passing my model to this viewuser manager? I know I can do this:
<p> Here a list of events: <% Html.RenderPartial("ListEvents", (new Model.Services.EventService(null)).ListEvents());%> </p>
But that doesn't seem like a very smart thing, creating a new model from the inside out? Or am I wrong here? I canβt even go through any confirmation, so the parameter is null. So an alternative is to store this data in the ViewData [] element, but my viewuser control should not depend on the ViewData of this parent!
I am sure that there is a very simple answer to this question, please share, since I did an online scan for this problem.
Thanks!
The simple answer : The viewusercontrol function should always get this model from the view it is in. Working around this, for example, adding a codebehind file to viewusercontrol will break the MVC pattern.
source share