这似乎是一个非常基本的问题,但我一直无法找到答案。我试图在我的一个控制器类中定义一个变量,以便对我的 [Bind(Include=...)] 属性设置采取“DRY”(不要重复自己)方法来设置我的操作方法。
我正在尝试这样做:
// Make the accessible fields more DRY
List<string> field_access = new List<string>();
field_access.Add("Title");
field_access.Add("Author");
field_access.Add("Genre");
field_access.Add("Level");
...
public ActionResult Create([Bind(Include = field_access)] Song song)
取而代之的是:
public ActionResult Create([Bind(Include = "ID,Title,Author,Genre,Level")] Song song)
这是错误:CS1519 Invalid token '(' in class, struct, or interface member declaration
指导将不胜感激。
错误 CS1519 意味着您的代码中有一些无效的令牌。
在这种情况下,您不需要使用列表。您可以在控制器中创建一个常量,然后在属性上使用它:
public class SongsController : Controller
{
private const string FieldAccess = "ID,Title,Author,Genre,Level";
public ActionResult Create([Bind(Include = FieldAccess)] Song song)
{
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句