2
<table>
    @foreach(var items in Model)
    {
        <tr>
            <td>@items.name</td>
            <td>@items.designation</td>
            <td>@items.salary</td>
        </tr>
    }
</table>

The code works fine when written directly on cshtml page.

If I use @Html.Raw(items.data) it shows the code instead of data/Values.

My requirement is to store the above code in database and display records stored in database and process it at runtime. I want to fetch data by the code.

Nikhil
  • 99
  • 1
  • 11

2 Answers2

3

You can use RazorEngine for templating: Your string in your database will be your template, then you can send your model as model for the template.

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

In the example above, the first param is your template, the second the model.

0

I don't think you can store razor code into database and reuse it. Because when you use @Html.Raw(), the view engine expects the string to be HTML code, and leaves it to be interpreted by the browser. If you keep razor codes there it goes straight to the browser. Expecting html syntaxes and getting razor ones, the browser gives you back the code...Hope it makes sense..

SamGhatak
  • 1,487
  • 1
  • 16
  • 27