简体   繁体   中英

Call Html.RenderAction from inside HTML string

I have a ASP.NET MVC 4 application that uses database to store pages (HTML), galleries, forms etc... I want to provide users a functionality to call other controller from inside pages.

Simplified problem is how to call render action from database acquired string. Just for example I would like that string contains @Html.RenderAction("Show", "Gallery", new {id=5})

Another option I have to parse string inside a controller and render all sub calls to string before rendering this HTML.

EDIT: The database would return something like code bellow, service layer can substitute {$gallery$} with @Html.RenderAction("Show", "Gallery", {id=5})

<div class="text">
<h1> title </h1>
<p> this is some random text {$gallery$} </p>
</div>

From your statement

Simplified problem is how to call render action from database acquired string.

I get that you want to call an action using dynamically provided action-name and controller . If this what you want you could get it using

ViewModel

 public class MyViewModel{

   public string Action {get;set;}

   public string ControllerName {get;set;}

 }

Controller

  public class MyController : Controller{

       public ActionResult MyView(){

         return View(new MyViewModel 
                   { Action ="MyPartialView" , ControllerName = "my"});
       }

       public ActionResult MyPartialView(){
         return PartialView();
       }
  }

View

 @model MyView

  ....render stuff for the view

@{
   Html.RenderAction(Model.Action,Model.ControllerName);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM