Asp – PartialView is not rendering

asp.net-mvc

In my View I have:

<% 
   foreach (Personas p in Model.Personas) {
       if (p.DisType == DisType.TextArea) {
 %>

<% Html.RenderPartial("~/Views/Shared/Controls/Custom/xxx/Textbox.ascx", p); %>

<%
       }
   }
 %>

First I'm not sure that I have the <% %> right.

Second, in my Partial View Textbox.ascx, I strongly typed it also to my ViewModel class just like I did in my View..the same ViewModel class. My ViewModel class has a Property of type Personas. So I assumed that since I'm passing a p (of type Personas) to my RenderPartial as the object to pass to it, that as long as I have strongly typed my Partial View and that object (which is the ViewModel clas) has that type in it (personas) that I can just do this now in my Partial View:

<fieldset>
    <div>
        <span>*</span><label><%=Model.Personas.Name %></label>
        <p><%=Model.Personas.Info %></p>
    </div>
    <div>
        <%=Html.TextBox(Model.Personas.Name, "",
                 new { name=Model.Personas.Name, id= Model.Personas.Id,
                       size = Model.Personas.Size1 })%>
    </div>
</fieldset>

Finally, so I tried all this. But nothing gets rendered. I don't know why. I don't know if I just have the syntax wrong in my View or I'm not getting valid data being passed, or that in my Partial View if I'm not wiring up to the passed object correctly.

Best Answer

Your assumption is incorrect. If you are passing a type of "Personas" to your partial view as it's model, then it needs to inherit System.Web.Mvc.ViewUserControl<Personas>, not the same type as the parent View. The partial view cannot just "know" that your parent view Model type has a property of type Personas and somehow map the object you pass into it to that property.

Also, since you have no literal text being rendered between your server tags, you can shorten your tags to simply:

<% 
   foreach (Personas p in Model.Personas) {
       if (p.DisType == DisType.TextArea) {

         Html.RenderPartial("~/Views/Shared/Controls/Custom/xxx/Textbox", p); 

       }
   }
 %>