[英]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.