如何分解数据以便将其输入到我的视图中?
我是 ASP.NET 的绝对初学者,我遇到了一个相当复杂的问题,特别是如何获取刚刚从数据库中获取的数据。下面是我的查询和处理数据的开始。
var brandslist = from brand in brandDB.brands
join brand_family in brandDB.brand_family
on brand.brand_family_id equals brand_family.bf_id
orderby brand_family.brand_family_name,
brand.priority,
brand.genre_id
select new
{
brand_family.brand_family_name,
brand.priority,
brand.roll_image
};
var viewModel = new BrandIndexViewModel{
Brands = brandslist.ToList()
};
我习惯用 PHP 做事,而不是 asp,这就是为什么我如此绝对难住的原因。我之前确实有一个有效的数据库调用,但我只从数据库中获取一项内容,而在这里我试图从数据库中获取多项内容。我想我的问题是如何使我的视图类可以访问数据?
感谢任何帮助我解决这个问题的人,因为我确信这是一个相当基本的问题。
编辑
下面是我在编译时收到的错误消息:
错误 1 无法隐式地将类型 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.List' C:\Inetpub\wwwroot\hcd\hcd\Controllers \HomeController.cs 35 26 hcd
编辑 2
只是为了确认我正在使用 ASP MVC 2。
编辑 3
这是我的品牌索引视图模型(但是我已经发现它可能存在问题)
public class BrandIndexViewModel
{
public List<string> Priority { get; set; }
public List<string> FamilyName { get; set; }
public List<string> RollImage { get; set; }
public List<string> Brands { get; set; }
}
I am an absolute beginner to asp.net and I've got quite a complex problem, specifically how do I get at the data I've just got from the database. Below is my query and the start of dealing with the data.
var brandslist = from brand in brandDB.brands
join brand_family in brandDB.brand_family
on brand.brand_family_id equals brand_family.bf_id
orderby brand_family.brand_family_name,
brand.priority,
brand.genre_id
select new
{
brand_family.brand_family_name,
brand.priority,
brand.roll_image
};
var viewModel = new BrandIndexViewModel{
Brands = brandslist.ToList()
};
I'm used to doing things with PHP, not asp which is why I'm so absolutely stumped. I do have one previous database call that works but i'm only getting one thing from the database whereas here I'm trying to get multiple items from the database. I suppose my question here is how do I make the data accessible to my view class?
Thanks to anyone who helps me out on this as I'm sure it's a fairly basic problem.
EDIT
Below is the error message I'm getting on compile:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' C:\Inetpub\wwwroot\hcd\hcd\Controllers\HomeController.cs 35 26 hcd
EDIT 2
Just to confirm I am using ASP MVC 2.
EDIT 3
This is my brand index view model (but i'm already seeing a possible problem with it)
public class BrandIndexViewModel
{
public List<string> Priority { get; set; }
public List<string> FamilyName { get; set; }
public List<string> RollImage { get; set; }
public List<string> Brands { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题的根源在于您的 LINQ 查询返回匿名类型,但您分配给 Brands 的是
List
。最终,您需要获得您想要的字符串。我假设“brand_family_name”是您想要进入List的数据。品牌
。The root of your problem is that your LINQ query returns an anonymous type, but you're assigning to Brands which is a
List<string>
. Ultimately, you need to get the strings you want. I'm going to assume that "brand_family_name" is the data that you want to go into theList<string> Brands
.正如其他答案中提到的,您没有在 ViewModel 的
Brands
列表中指定您想要的内容(拥有类似brand.Name
之类的内容是最合乎逻辑的) 。发布您的brand
和brand_family
类可以澄清这一点。如果我理解正确的话,您基本上需要一个
Brands
列表来显示在您的视图中。在这种情况下,将FamilyName
、Priority
、RollImage
和Brand
分组到一个对象中并拥有一个对象会更简洁。 ViewModel 中的列表:在这种情况下,您不必执行
Select
来填充每个列表。相反,您可以将查询结果直接投影到 ViewModel 上。使用连接后的代码应如下所示:此外,您提到使用数据库,但没有提及是否使用 ORM(例如 EF 或 LINQ-to-SQL)。如果您正在使用其中之一,如果您可以发布模型的屏幕截图,那将会非常有帮助。同时,根据您的代码,您似乎在
brand_family
和brand
之间存在一对多关系,因此,如果您使用 ORM,您可能有导航属性brand.brand_family
。在这种情况下,您的 LINQ 查询中不需要join
:As mentioned in the other answers, you don't specify what you want in the
Brands
list of your ViewModel (it would be most logical to have something likebrand.Name
). Posting yourbrand
andbrand_family
classes could clarify this.If I understand correctly, you basically need a list of
Brands
to display in your View. In that case it would be neater to groupFamilyName
,Priority
,RollImage
andBrand
into one object and have a list of those in your ViewModel:In that case you won't have to do a
Select
in order to populate every list. Instead, you can project the query results directly onto your ViewModel. Here is what the code should look like with the join:Furthermore, you mention using a Database, but you don't mention whether you are using an ORM such as EF or LINQ-to-SQL. If you are using one of them, it would really help if you could post a screenshot of your model. Meanwhile, according to your code, it does seem that you have a one-to-many relationship between
brand_family
andbrand
, so if you are using an ORM, you probably have a navigational propertybrand.brand_family
. In that case, you won't need ajoin
in your LINQ query:我假设您正在使用 ASP.NET MVC。
要从您的视图访问 Brandslist 变量,请使用模型参数从控制器将其传入。例如:
那么在您的视图中,将有一个名为“Model”的变量,其中将包含该值。然后,您可以使用 for/each 循环遍历 .ASPX 代码中的每条记录。
I'm going to assume that you are using ASP.NET MVC.
To access the brandslist variable from your view, pass it in from the controller using the model parameter. For example:
Then in your view, there will be a variable called "Model" which will contain this value. You can then iterate through each record in the .ASPX code using a for/each loop.