ASP.Net MVC ViewUserControl with a controller using MVC 1.0

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.

+4
source share
1 answer

By default, the same model will be used as the page. If you want to provide a model for each instance of RenderPartial , your situation is probably similar to rendering multiple entries in a blog application. You can get each model from the collection in your page model and pass it to the user control as follows:

 foreach (var post in Model.Entries) { Html.RenderPartial("PostTemplate", post); } 
+2
source

All Articles