Search code examples
c#asp.net-corerazor-pages

Add control dynamically to razor page using C#.NET Core 3.1


I am building a web application using the ASP.NET C# Core 3.1 MVC and Razor pages.

I am new to Razor pages.

I want to add a button dynamically to the razor page through c#.net core backend code.

I have following sample ASP.NET code syntax that adds the html control present in the string "strForm", to the asp page.

Page.Controls.Add(new LiteralControl(strForm));

What is the equivalent of the above code in C#.NET Core 3.1?


Solution

  • MVC doesn't use a control tree; you just need to return the raw HTML to the user: C#

    We have to do the following - Replace

    Page.Controls.Add(new LiteralControl(strForm));
    

    By

    return Content(strForm, System.Net.Mime.MediaTypeNames.Text.Html);
    

    Please refer url for reference -

    "https://www.codeproject.com/Questions/1230430/Unable-to-redirect-user-to-payment-page-of-the-pay"