将变量添加到 .NET MVC 控制器
Add Variable to .NET MVC Controller
这似乎是一个非常基本的问题,但我一直找不到答案。我试图在我的一个控制器 classes 中定义一个变量,以便对我的 [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 class、结构或接口成员声明中的无效标记“(”
非常感谢指导。
错误 CS1519 表示您的代码中有一些无效令牌。
在这种情况下您不需要使用列表。您可以在控制器中创建一个常量,然后在属性上使用它:
public class SongsController : Controller
{
private const string FieldAccess = "ID,Title,Author,Genre,Level";
public ActionResult Create([Bind(Include = FieldAccess)] Song song)
{
}
}
这似乎是一个非常基本的问题,但我一直找不到答案。我试图在我的一个控制器 classes 中定义一个变量,以便对我的 [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 class、结构或接口成员声明中的无效标记“(”
非常感谢指导。
错误 CS1519 表示您的代码中有一些无效令牌。
在这种情况下您不需要使用列表。您可以在控制器中创建一个常量,然后在属性上使用它:
public class SongsController : Controller
{
private const string FieldAccess = "ID,Title,Author,Genre,Level";
public ActionResult Create([Bind(Include = FieldAccess)] Song song)
{
}
}