从数据库或其他来源渲染剃刀 [英] Render razor from database or other sources

查看:65
本文介绍了从数据库或其他来源渲染剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将剃刀代码从数据库渲染到剃刀视图.有可能吗?

I would like to render the razor code from the database to to a razor view. Would thatbe possible?

CONTROLLER/ACTION:

public ActionResult About()
{
    ViewBag.Message = "Your application description page.";
    ViewBag.RazorCode = "@Html.TextBox(\"txtTestRazor\")";
    return View();
}

查看:

@{
    ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

<h3>@ViewBag.RazorCode</h3>

HTML/输出:

关于.

您的应用程序描述页面.

Your application description page.

使用此区域提供其他信息.
@ Html.TextBox("txtTestRazor")

Use this area to provide additional information.
@Html.TextBox("txtTestRazor")

推荐答案

一种选择是使用 RazorEngine

基础非常简单,只需给它一个字符串(和一个模型)

Basics are very easy, just give it a string (and a model)

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

它不支持html帮助器,路由等.(据我所知).

It doesn't support html helpers, routing etc. out of the box (as far as I remember).

这篇关于从数据库或其他来源渲染剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆