asp.netasp.net-mvcrepeaterhtmlextensions

Passing Container.Eval to (Html.ReaderPartial) inside ASP.NET Repeater Control


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.


Solution

  • 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...