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

How to embed razor variable in string?

我想将 razor 模型变量添加到资源文件字符串中。我尝试了以下但变量呈现为文字:

尝试 1:

"There is a variable @Model.here"

尝试 2:

"There is a variable @(Model.here)"

代码中是这样引用的:

@MyManager.GetString("resourcefilevariable")

有什么办法吗?

做这种事情还是存储好

"There is a variable {0}"

作为资源字符串,在视图中,这样写:

@string.Format(Resources.String, model.here);

作为一个完整的例子:

这是模型class:

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

    public Foo()
    {
        Name = "bar";
    }
}

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

    // GET: Foo
    public ActionResult Index()
    {
        return View(new Foo());
    }

有一个资源为

的 resx 文件
[resourceName, <strong>name of model is: {0}</strong>]

在剃须刀视图中,将其呈现为

@Html.Raw(string.Format(Resources.resourceName, Model.Name))

正如 Leigh Shepperson 指出的那样,您可以使用带占位符的字符串和 string.Format 将占位符替换为实际值。如果字符串包含任何 HTML 标记,则应使用 Html.Raw 方法呈现。

@Html.Raw(string.Format(Resources.String, model.here))