Because of the confusion between all the info that is out there about mvc from all the preview releases and the one official release I am very confused 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 needs to be displayed on several pages of my website. Therefore I have put a new ViewUserControl (ListEvents.ascx) inside my Views\Shared folder.
I am requesting this ListEvents.ascx to render on my Home/Index view like this:
<p>
Here's a list of events:
<% Html.RenderPartial("ListEvents");%>
</p>
How would I go about passing my model to this viewusercontrol? I know I can do this:
<p>
Here's a list of events:
<% Html.RenderPartial("ListEvents", (new Model.Services.EventService(null)).ListEvents());%>
</p>
But that doesn't seem like a very smart thing to do, creating a new model from inside a view?! Or am I wrong here? I can't even pass any validationstate, hence the null parameter. So an alternative option is to store this data into the ViewData[] member, but my viewusercontrol is not supposed to be dependant on the ViewData of it's parent!
I'm sure there is a very simple answer to this, please share as I'm done browsing the web for this problem.
Thanks!
Simple Answer: A viewusercontrol should always receive it's model from the View in which it resides. Working around this, like by adding a codebehind file to a viewusercontrol, would break the MVC pattern.