简体   繁体   English

为什么我的MVC剃刀视图不希望实例化ResourceManager

[英]Why does my MVC razor view does not want to instantiate a ResourceManager

I'm writing this code on an MVC 3 view: 我在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>

For some reason the line with "@new ResourceManager..." is not recognized as valid and it gives me a Compilation Error whenever I run it. 由于某些原因,带有“ @new ResourceManager ...”的行未被识别为有效行,并且每当我运行它时,都会给我一个编译错误。

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. 如果我只写@ResourceManager,文本将变成蓝色,指示被识别为有效类,对于IndexStrings资源也是如此,但是从整体上看,似乎并不知道这些应该是类。

What am I doing wrong? 我究竟做错了什么?

Thank you. 谢谢。

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: 您可能还想考虑添加一个辅助方法,也许作为HtmlHelper的扩展方法,因此可以使用以下方法:

@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());
}

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

相关问题 为什么我的MVC Razor仅在第一次返回视图时才填充HTML表? - Why does my MVC Razor populate the HTML table only the first time the view is returned? 为什么我的Razor“ RenderSection”没有被孙子视图继承? - Why does my Razor 'RenderSection' not get inherited by a grandchild view? 为什么我的 razor 视图会为代码的错误部分抛出 InvalidOperationException? - Why does my razor view throw InvalidOperationException for the wrong part of the code? 为什么Modelbinder在ASP.NET MVC4中为我的列表实例化空对象? - Why does the modelbinder instantiate empty objects for my list in ASP.NET MVC4? 为什么我的下拉列表在MVC的部分视图中不起作用? - Why my Dropdownlist does not work in Partial View in MVC? MVC Partial View呈现但不执行Razor代码 - MVC Partial View renders but does not execute Razor code MVC3剃刀局部视图不会发送回控制器 - MVC3 Razor Partial View does not send back to controller 选中复选框后,表中的MVC Razor视图文本不会更新 - MVC Razor View Text in table does not update on checkbox selection 为什么剃刀不喜欢这个? - Why does Razor not like this? 在剃刀视图中实例化一个对象Asp.net Mvc 4 - Instantiate an object in razor view Asp.net Mvc 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM