简体   繁体   English

从HTML字符串内部调用Html.RenderAction

[英]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. 我有一个ASP.NET MVC 4应用程序,该应用程序使用数据库存储页面(HTML),图库,表单等。我想为用户提供从页面内部调用其他控制器的功能。

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}) 例如,我希望该字符串包含@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. 另一个选项是我必须解析控制器内部的字符串,并在呈现此HTML之前将所有子调用呈现给字符串。

EDIT: The database would return something like code bellow, service layer can substitute {$gallery$} with @Html.RenderAction("Show", "Gallery", {id=5}) 编辑:数据库将返回类似下面的代码,服务层可以将{$ gallery $}替换为@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 . 我知道您想使用动态提供的action-namecontroller调用一个动作。 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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