为什么我的 MVC razor 视图不想实例化 ResourceManager

发布于 2024-12-19 19:02:32 字数 876 浏览 2 评论 0原文

我在 MVC 3 视图上编写此代码:

<table>
    <tr>
        <th>@SecondIndexStrings.Activity_ActivityName</th>
        <th>@SecondIndexStrings.Activity_DateStarted</th>
        <th>@SecondIndexStrings.Activity_ProficiencyLevel</th>
    </tr>
    @foreach (var activity in Model.Activities)
    {
        <tr>
            <td>@activity.ActivityName</td>
            <td>@activity.DateStarted.ToShortDateString()</td>
            <td>@new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString())</td>
        </tr>
    }
</table>

由于某种原因,带有“@new ResourceManager...”的行未被识别为有效,并且每当我运行它时都会出现编译错误。

如果我只写@ResourceManager,文本会变成蓝色,表明它被识别为有效的类,对于 IndexStrings 资源也是如此,但是,从整体来看,它似乎不知道这些应该是类。

我做错了什么?

谢谢。

I'm writing this code on an MVC 3 view:

<table>
    <tr>
        <th>@SecondIndexStrings.Activity_ActivityName</th>
        <th>@SecondIndexStrings.Activity_DateStarted</th>
        <th>@SecondIndexStrings.Activity_ProficiencyLevel</th>
    </tr>
    @foreach (var activity in Model.Activities)
    {
        <tr>
            <td>@activity.ActivityName</td>
            <td>@activity.DateStarted.ToShortDateString()</td>
            <td>@new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString())</td>
        </tr>
    }
</table>

For some reason the line with "@new ResourceManager..." is not recognized as valid and it gives me a Compilation Error whenever I run it.

If I only write @ResourceManager the text turns blue indicating that is recognized as a valid class, the same for the IndexStrings resource but, with the whole thing it seems that it doesn't know that those are supposed to be classes.

What am I doing wrong?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2024-12-26 19:02:32

由于存在空格,您必须使用括号来帮助它查看表达式的开始/结束位置:

@(new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString()))

您可能还需要考虑添加一个辅助方法,也许作为 HtmlHelper 上的扩展方法,因此你可以使用类似:

@Html.Resource<IndexStrings>(activity.ProficiencyLevel)

或类似的东西,它可能看起来像:

public static string Resource<T>(this HtmlHelper html, object key) {
    return new ResourceManager(typeof(T)).GetString(key.ToString());
}

Because of the white-space, you must use parenthesis to help it see where your expression starts/ends:

@(new ResourceManager(typeof(IndexStrings)).GetString(activity.ProficiencyLevel.ToString()))

You might also want to consider adding a helper method, perhaps as an extension-method on HtmlHelper, so you can use something like:

@Html.Resource<IndexStrings>(activity.ProficiencyLevel)

or similar, which might look something like:

public static string Resource<T>(this HtmlHelper html, object key) {
    return new ResourceManager(typeof(T)).GetString(key.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文