tags:

views:

39

answers:

2

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.

+1  A: 

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); 

       }
   }
 %>
womp
Thanks and I forgot to add .ascx to the RenderPartial tag. I changed my PartialView to expect type Personas and still, and got rid of the unneccessary <% %>, but I get no rendering of anything.
CoffeeAddict
What does your partial view look like? Can you just try putting "hello" or something in it without any server tags and still get nothing?
womp
And let me ask, what if you wanted to send multiple types to your RenderView. Are you saying to now create yet another ViewModel class to handle going the other way!?!? that sounds messy to me.
CoffeeAddict
The partial view code is above...but yea, I will try a simple text to see if it renders
CoffeeAddict
rendering a simple string works fine.
CoffeeAddict
Is there any way to debug a View during compile time?
CoffeeAddict
figured it out, my if statement was evaluating to false
CoffeeAddict
If the time I spent helping you was appreciated, could you take the time to upvote my posts and/or accept them as answers? Thanks!
womp
+1  A: 

View should inherit from ViewUserControl<Personas>

and within the wiew the model points to eact instance of personas

<span>*</span><label><%=Model.Name %></label>
Rony