I am trying to understand how ASP.NET MVC works and I have been recommended to follow the MVC Music Store Tutorial, however I am having problems since I am using different (more current) software.
我试图理解ASP.NET MVC是如何工作的,我被推荐遵循MVC音乐商店教程,但是由于我使用的是不同的(更新的)软件,我遇到了问题。
According to this page I should add a List
of genres to my action method inside my StoreController.cs
. However according to Visual studio, this piece of code seems to be incorrect or not recognized. The error says: Identifier expected;'new' is a keyword
. Should I use different code or do this in my model class instead in some way?
根据这个页面,我应该在我的StoreController.cs中为我的action方法添加一个类型列表。然而根据Visual studio,这段代码似乎不正确或无法识别。错误说:标识符预期;'new'是关键字。我应该使用不同的代码,还是以某种方式在我的模型类中执行此操作?
public ActionResult Index()
{
var genres = new List<Genre>
{
new Genre = { Name = "Disco" },
new Genre = { Name = "Jazz" },
new Genre = { Name = "Rock" }
};
return View();
}
Shouldn't something like this do the job?:
不应该这样做这个工作吗?:
public ActionResult Index()
{
//var genres = new List<Genre>
//{
// new Genre = { Name = "Disco" },
// new Genre = { Name = "Jazz" },
// new Genre = { Name = "Rock" }
//};
var genres = new List<Genre>();
genres.Add(new Genre() { Name = "Disco" });
genres.Add(new Genre() { Name = "Jazz" });
genres.Add(new Genre() { Name = "Rock" });
return View();
}
And doesn't this constantly add the genres everytime I would run through the Index action method?
并且每次我通过Index动作方法运行时,这不会不断添加类型吗?
2 个解决方案
#1
5
Your syntax is incorrect. Your first snippet is an object initializer and is no different than your second block of code where you create a new instance of Genre
and assign its Name
property, only in the first case you are attempting to assign { Name = "Disco" }
to new Genre()
.
你的语法不正确。您的第一个代码段是一个对象初始值设定项,与您创建Genre的新实例并分配其Name属性的第二个代码块没有什么不同,仅在第一种情况下,您尝试将{Name =“Disco”}分配给new类型()。
Read more on Object Initializer
阅读有关Object Initializer的更多信息
public ActionResult Index()
{
var genres = new List<Genre>
{
new Genre { Name = "Disco" },
new Genre { Name = "Jazz" },
new Genre { Name = "Rock" }
};
return View();
}
#2
2
To answer your second question, yes, that list (which is entirely in memory) will get created and then discarded every time your action runs. It's only meant to be sample code, if your application is more complicated you will probably get lists like this from somewhere else. Also, the code doesn't seem to do anything with this list, but I assume that's left out - you probably want it as part of the model or viewbag etc.
要回答第二个问题,是的,该列表(完全在内存中)将被创建,然后在每次运行时丢弃。它只是示例代码,如果您的应用程序更复杂,您可能会从其他地方获得这样的列表。此外,代码似乎没有对此列表做任何事情,但我认为这是遗漏的 - 您可能希望它作为模型或视图包等的一部分。
#1
5
Your syntax is incorrect. Your first snippet is an object initializer and is no different than your second block of code where you create a new instance of Genre
and assign its Name
property, only in the first case you are attempting to assign { Name = "Disco" }
to new Genre()
.
你的语法不正确。您的第一个代码段是一个对象初始值设定项,与您创建Genre的新实例并分配其Name属性的第二个代码块没有什么不同,仅在第一种情况下,您尝试将{Name =“Disco”}分配给new类型()。
Read more on Object Initializer
阅读有关Object Initializer的更多信息
public ActionResult Index()
{
var genres = new List<Genre>
{
new Genre { Name = "Disco" },
new Genre { Name = "Jazz" },
new Genre { Name = "Rock" }
};
return View();
}
#2
2
To answer your second question, yes, that list (which is entirely in memory) will get created and then discarded every time your action runs. It's only meant to be sample code, if your application is more complicated you will probably get lists like this from somewhere else. Also, the code doesn't seem to do anything with this list, but I assume that's left out - you probably want it as part of the model or viewbag etc.
要回答第二个问题,是的,该列表(完全在内存中)将被创建,然后在每次运行时丢弃。它只是示例代码,如果您的应用程序更复杂,您可能会从其他地方获得这样的列表。此外,代码似乎没有对此列表做任何事情,但我认为这是遗漏的 - 您可能希望它作为模型或视图包等的一部分。