views:

1318

answers:

1

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.

+2  A: 

By default, the same model as the page will be used. If you want to provide a model to each instance of RenderPartial, your situation is probably like rendering several entries in a blog application. You could fetch each model from a collection in your page model and pass it to the user control like this:

foreach (var post in Model.Entries) {
  Html.RenderPartial("PostTemplate", post);
}
Mehrdad Afshari
Yes but then my viewusercontrol's model is still dependant on the model of the page it resides in. And that's exactly what I want to avoid.
Peter
They actually depend on a single property of your model, not the model itself. Two completely distinct models might have a same property.
Mehrdad Afshari
So everytime I need to implement a viewusercontrol into a view, I need to make sure that the controller for that view appends the view's model with the model that my viewusercontrol uses?
Peter
Definitely. Basically, it should already do it in some way. Your controller should have already passed the data required to display the view. You are just giving it a structure that facilates encapsulating a part in a separate control.
Mehrdad Afshari
I see, thanks for the clear explanation. I assume it is part of the MVC pattern that you can not have a viewusercontrol that acts as an island on it's own, with it's own adapter?
Peter
its own controller*
Peter
Yeah. MVC pattern restricts you not to fetch any data directly from View objects. You should, however, always see if the going with the pattern is really worth it. If it isn't, don't go with it just for the sake of "implementing the pattern."
Mehrdad Afshari
no I chose this pattern by using asp.net MVC, so I am gonna stick with it and apply all it's rules. Makes it easier for future developers and keeps things clean. Thanks for your quick responses!
Peter