Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC ViewUserControl with controller using MVC 1.0
    text
    copied!<p>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:</p> <p>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.</p> <p>I am requesting this ListEvents.ascx to render on my Home/Index view like this:</p> <pre><code>&lt;p&gt; Here's a list of events: &lt;% Html.RenderPartial("ListEvents");%&gt; &lt;/p&gt; </code></pre> <p>How would I go about passing my model to this viewusercontrol? I know I can do this:</p> <pre><code>&lt;p&gt; Here's a list of events: &lt;% Html.RenderPartial("ListEvents", (new Model.Services.EventService(null)).ListEvents());%&gt; &lt;/p&gt; </code></pre> <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!</p> <p>I'm sure there is a very simple answer to this, please share as I'm done browsing the web for this problem.</p> <p>Thanks!</p> <p><strong>Simple Answer</strong>: 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload