我可以从MVC中的视图访问资源文件吗? [英] Can I access Resources file from a View in MVC?

查看:303
本文介绍了我可以从MVC中的视图访问资源文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用存储在资源文件中的值填充下拉列表。最好的方法是什么?



我可以创建一个SelectList并将其推送到Model,在这种情况下,下拉列表将被自动填充。
但是我可以从视图访问资源文件吗?如果是的话,我应该吗?所有的资源字符串都被编译成一个类,你可以在你的视图中引用这个类。 示例:

 <%= Resources.Strings.MyCustomString%> 

我相信以下内容会自动添加到您的web.config中,因此您可以删除资源..

 < namespaces> 
< add namespace =资源>
< /命名空间>

但是,这不支持本地化。为此,您需要使用帮助程序方法

如果你想填充一个列表,你需要创建一个帮助类,它可以遍历字符串类并提取适当的值或者编码在逗号分隔的列表中选择您的选择,并在将其提供给您的下拉列表的选择列表之前解析/分割。


I want to populate dropdownlist with values stored in a resource file. What's the best approach for this?

I can create a SelectList and push it in Model in which case dropdown would be populated automatically. But can I access resource file from a View ? If yes, should I?

解决方案

All resource strings get compiled into a class which you can reference in your views. Example:

<%= Resources.Strings.MyCustomString %>

I believe the following is automatically added to your web.config so you can drop the Resources..

<namespaces>
    <add namespace="Resources">
</namespaces>

However, this will not support localization. For that you'll want to use a helper method.

If you're trying to populate a list you'll need to create a helper class that can iterate through the Strings class and extract the appropriate values or encode your selections in a comma delimited list and parse/split that before feeding it to your dropdownlist's selectionlist.

这篇关于我可以从MVC中的视图访问资源文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆