简体   繁体   English

在字符串中包含Razor语法

[英]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. 我试图通过在控制器中构建html,然后在视图中呈现它来动态创建标签。 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. 我遇到的问题是@ Html.RenderAction代码被解释为字符串而不是Razor语法。

 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 它显示在我看来,这样的.. 观点

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. 简短的回答:您不能将Razor代码作为字符串从控制器传递到View,因为它将被解释为变量的值,但不能作为要运行的代码。

Long answer: Your attempt shows a major misunderstanding of the ASP.NET MVC Framework. 长答案:您的尝试表明对ASP.NET MVC框架存在重大误解。 It is build upon the MVC pattern, for Model View Controller. 它基于MVC模式(用于模型视图控制器)构建。 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. 视图负责构建HTML,而不负责控制器。 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. 更具体地说:View的目标是构造HTML,并将其发送回客户端。 The View is a .cshtml file, ie a Razor file. 视图是.cshtml文件,即Razor文件。 Razor is a language very close to the syntax of C#, which can be mixed with HTML, and used to build HTML. Razor是一种非常接近C#语法的语言,可以与HTML混合使用,并用于构建HTML。 The View is NOT the HTML yet! 该视图还不是HTML!

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: 让我们举个例子:控制器传递给视图的数据可以是List<string>而Razor代码可以通过以下方式为List中的每个string生成一个新的HTML段落:

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

This Razor code needs to be run, and the resulting HTML would be: 此Razor代码需要运行,并且产生的HTML将是:

<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. 请注意,字符串的内容是Razor解析器的简单值,而不是要解析的代码。 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. 由于它不是HTML,因此客户端浏览器也不会解释它,只有基于ASP.NET MVC的服务器才能解释Razor代码。

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: Foo.cshtml文件中的View应该只是这样:

<!-- 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>

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

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