Jason Davidson
Jason Davidson

Reputation: 13

Include Razor Syntax in string

I'm trying to dynamically create tabs by building the html in the controller and then rendering it in the view. It all works, but for the tab content I would like it to render an action.

The issue i'm having is that the @Html.RenderAction code is being interpreted as a string and not as Razor syntax.

 model.TabHtml = model.TabHtml + @"<h3>" + role.title + "</h3>";
 model.TabHtml = model.TabHtml + @"@Html.RenderAction(""Index"", ""Matrix"", new {id = " + role.id + "})" ;
 model.TabHtml = model.TabHtml + @"</div>";

Is there any solution to this or perhaps even an alternative way of going about this?

It displays in my view like this .. view

Upvotes: 0

Views: 1179

Answers (1)

Laurent Gabiot
Laurent Gabiot

Reputation: 1301

Short answer: You cannot pass Razor code as string from your controller to your View, as it will be interpreted a the value of a variable, but not as code to be run.

Long answer: Your attempt shows a major misunderstanding of the ASP.NET MVC Framework. It is build upon the MVC pattern, for Model View Controller. While it is possible to write code that do not strictly follow the pattern, your example shows a way that simply mis-interpret the pattern. The View is responsible of the building of your HTML, not the controller. The Controller is selecting the right View, and passes to it the right data from the Model. You should reframe your problem in this regard.

More specificly: The goal of the View is to construct HTML, that can be send back to the client. The View is a .cshtml file, i.e. a Razor file. Razor is a language very close to the syntax of C#, which can be mixed with HTML, and used to build HTML. The View is NOT the HTML yet!

Let's take an example: The data passed by the Controller to the View could be a List<string> and your Razor code could generate a new HTML paragraph for each string in your List in the following way:

@foreach(string s in ListOfString)
{
    <p>s</p>
}

This Razor code needs to be run, and the resulting HTML would be:

<p>content of the first string</p>
<p>content of the second string</p>
<p>content of the third string</p>
<p>content of the fourth string</p>

Notice that the content of the strings are simple values to the Razor parser, not code to be parsed. That explains why this code is then displayed on your page: it will never be parsed as code.

Since it is not HTML, it won't be interpreted by the client browser neither, only the ASP.NET MVC based server is able to interpret Razor code.

A correct way of using the Pattern would be:

In the Controller:

public IActionResult Foo()
{
    // Construct your data from Model here, and store it in a variable Bar of type MyType.
    Mytype Bar = SomeComputing(MyModel);
    // For instance Bar could have properties called Prop1, Prop2, Prop3, etc...
    return(Bar);
}

and the View, in the Foo.cshtml file, should simply be something like:

<!-- using the Bar data from the Controller -->
@model MyType 

<div class="@Model.Prop1">
    <h3>@Model.Prop2</h3>
    <p>@Model.Prop3</p>
    <a href="@Model.Prop4">Link</a>
</div>

Upvotes: 1

Related Questions