如何在字符串中嵌入剃须刀变量?

4th*_*ace 2 c# asp.net-mvc razor razorengine

我想将剃刀模型变量添加到资源文件字符串中。我已经尝试了以下方法,但是变量呈现为文字:

尝试1:

"There is a variable @Model.here"
Run Code Online (Sandbox Code Playgroud)

尝试2:

"There is a variable @(Model.here)"
Run Code Online (Sandbox Code Playgroud)

在代码中,引用如下:

@MyManager.GetString("resourcefilevariable")
Run Code Online (Sandbox Code Playgroud)

有什么办法可以做到这一点?

Lei*_*son 5

最好通过存储来做这种事情

"There is a variable {0}"
Run Code Online (Sandbox Code Playgroud)

作为资源字符串,并在视图中编写如下内容:

@string.Format(Resources.String, model.here);
Run Code Online (Sandbox Code Playgroud)

举一个完整的例子:

这是模型类:

public class Foo
{
    public string Name { get; set; }

    public Foo()
    {
        Name = "bar";
    }
}
Run Code Online (Sandbox Code Playgroud)

它具有带有简单Index ActionResult的控制器:

    // GET: Foo
    public ActionResult Index()
    {
        return View(new Foo());
    }
Run Code Online (Sandbox Code Playgroud)

有一个带有资源的resx文件

[resourceName, <strong>name of model is: {0}</strong>]
Run Code Online (Sandbox Code Playgroud)

在剃刀视图中,将其渲染为

@Html.Raw(string.Format(Resources.resourceName, Model.Name))
Run Code Online (Sandbox Code Playgroud)

  • 我认为这不会起作用,因为没有引用资源文件变量。我已经用那个细节更新了 OP。 (2认同)