1

I have a web forms project, gone MVC. That means I have:

  • A C# project with controllers and viewmodels
  • A VB.NET Web Forms project, that has some views

All our logic is in the controllers, and in code behind from our Web From views, we call the controllers and use the viewmodels.

In my case, I have a ContentController that does a lot of logic.

What I need to do is to render a user control and get the HTML. Problem is, I have not added the System.Web.UI DLL, and my partials are in the VB.NET project.

Therefore, I want to do the following:

In my viewmodel, i have a property called Content. In my content I want to inject a string like this:

var res = "<%=Html.Render(\"~\\Views\\Products\\_ProductRecListItemView.ascx\", p)%>";

And then set a value called p in my viewmodel, and then set that value like this:

        ProductController productController = new ProductController();
        var p_model = productController.Get("9788711401262");
        model.p = p_model;

Now, my problem is when I layout this in a <p>@Model.Content</p>, it obviously ends up looking like this as it is treated as a string on my page:

enter image description here

So my question is... How do I render this string as code, so I actually load my user control?

For reference, this is my "view":

<asp:Content ID="Content4" ContentPlaceHolderID="ContentMain" runat="server">
    <div class="cmsSection">

        <h1><%=Model.Headline%></h1>

        <div>
            <%=Model.Content%>
        </div>
    </div>
</asp:Content>
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • Try using @Html.Raw( Model.Content ) instead. – Cybrosys Nov 11 '14 at 06:46
  • It's not clear what you have and what you wish for. Are you looking for the standard TemplateControl.LoadControl method? http://stackoverflow.com/questions/16096458/loadcontrol-usercontrol-in-webmethod – Simon Mourier Nov 13 '14 at 06:49
  • Web forms Usercontrols require view state. In case, if you load usercontrols in mvc view it would fail when it depends on ViewState. If your userControl not using ViewState then it should be fine. Coming to Rendering UserControl in MVC View is doable. Look at this qn http://stackoverflow.com/questions/6317317/mvc-3-add-usercontrol-to-razor-view Are you still you're looking for answer? – Koti Panga Nov 13 '14 at 21:55

3 Answers3

1

asp.net web forms has LoadControl method to render user control.

Control TemplateControl.LoadControl(Type t, object[] parameters)

you can directly call-

LoadControl("~/control.ascx");
Kawsar Hamid
  • 514
  • 3
  • 13
0

As I commented above, try using Html.Raw or MvcHtmlString.Create.

Plus, I don't think it's possible because the html you want to inject contains server controls which themselves generate html. You could try one of the methods above and output the contents within these tags <% @Html.Raw(Model.Content) %> so the server knows to process/execute the Model.Content.

Cybrosys
  • 374
  • 2
  • 11
  • It is a Web Forms project, so I don't have access to Html.Raw :) But I will see if I can reproduce the Html.Raw function in my Html helpers – Lars Holdgaard Nov 12 '14 at 10:45
  • Potential alternative to html raw: http://stackoverflow.com/questions/10819655/alternative-of-html-raw-in-asp-net-webforms How to load an ascx control in web forms: http://stackoverflow.com/questions/1290592/dynamically-load-a-user-control-ascx-in-a-asp-net-website . Combine the two. But in your question you say

    @Model.Content

    returns the text of the string, if @Model.Content is working, razor is active, so html.raw will be available???
    – RandomUs1r Nov 13 '14 at 22:38
0

Server.HtmlDecode(DataContent.FieldValue("Contents", Container))

Puneet_2696717
  • 221
  • 6
  • 14