2

I am trying to pass Eval to Html.RenderPartial inside ASP.NET Repeater but it does not work can any one help?

<asp:Repeater runat="server">
            <ItemTemplate>
                <% Html.RenderPartial("UserControl1",Eval("Title")); %>
            </ItemTemplate>
</asp:Repeater>

by the way I know that I can do it in other ways but I want to know if it is doable or not.

Khaled Musaied
  • 2,513
  • 3
  • 25
  • 38
  • I know you can compbine WinForms with MVC... But man... I wouldn't recommend it. You're going to end up in a place you don't want to be. Maybe try putting your RenderPartial inside <%# %> statement. – Robert Koritnik Jul 14 '09 at 22:06

2 Answers2

5

is the same as in that it expects an expression that returns a string, so to get this compiling you have to call a method that calls Html.RenderPartial(), then returns an empty string:

<%
protected string RenderControl(object dataItem) 
{
    Html.RenderPartial("UserControl1", ((MyType) dataItem).Title);
    return "";
}
%>

... <%# RenderControl(Container.DataItem) %> ...

I would just use foreach though - mixing WebForms data-binding and MVC partial rendering is unpredictable, at best:

<% foreach (MyObject o in data) { Html.RenderPartial("UserControl1", o.Title); } %>

Don't make life any harder than it needs to be...

Sam
  • 6,167
  • 30
  • 39
  • Every thing is easy but <% Html.RenderPartial("UserControl1", o.Title);%> is making it hard it is different from other html extentions Thanks anyway – Khaled Musaied Jul 15 '09 at 15:04
0

Try putting your RenderPartial inside <%# %> statement like:

<asp:Repeater runat="server">
    <ItemTemplate>
        <%# Html.RenderPartial("UserControl1",Eval("Title")); %>
    </ItemTemplate>
</asp:Repeater>
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Thanks for your help but I got this compilation exception: CS1026: ) expected I test it before it wont work thanks again... – Khaled Musaied Jul 14 '09 at 22:17