如何分解数据以便将其输入到我的视图中?

发布于 2024-11-01 06:28:41 字数 1537 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2024-11-08 06:28:41

问题的根源在于您的 LINQ 查询返回匿名类型,但您分配给 Brands 的是 List。最终,您需要获得您想要的字符串。我假设“brand_family_name”是您想要进入 List的数据。品牌

var viewModel = new BrandIndexViewModel{
            Brands = brandslist.Select(b => b.brand_family_name).ToList() // Or whichever property you want to use to populate List<string> Brands
        };

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 the List<string> Brands.

var viewModel = new BrandIndexViewModel{
            Brands = brandslist.Select(b => b.brand_family_name).ToList() // Or whichever property you want to use to populate List<string> Brands
        };
柒七 2024-11-08 06:28:41

正如其他答案中提到的,您没有在 ViewModel 的 Brands 列表中指定您想要的内容(拥有类似 brand.Name 之类的内容是最合乎逻辑的) 。发布您的 brandbrand_family 类可以澄清这一点。

如果我理解正确的话,您基本上需要一个 Brands 列表来显示在您的视图中。在这种情况下,将 FamilyNamePriorityRollImageBrand 分组到一个对象中并拥有一个对象会更简洁。 ViewModel 中的列表:

public class BrandIndexViewModel
{
    public List<BrandIndexItem> BrandIndexItems { get; set; }
}

public class BrandIndexItem
{
    public string Priority { get; set; }
    public string FamilyName { get; set; }
    public string RollImage { get; set; }
    public string Brand { get; set; }
}

在这种情况下,您不必执行 Select 来填充每个列表。相反,您可以将查询结果直接投影到 ViewModel 上。使用连接后的代码应如下所示:

    var viewModel = new BrandIndexViewModel
                    {
                        BrandIndexItems = 
                           (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 BrandIndexItem
                            {
                                FamilyName = brand_family.brand_family_name,
                                Priority = brand.priority,
                                RollImage = brand.roll_image,
                                Brands = brand.name // ?
                            }).ToList()
                    }    

此外,您提到使用数据库,但没有提及是否使用 ORM(例如 EF 或 LINQ-to-SQL)。如果您正在使用其中之一,如果您可以发布模型的屏幕截图,那将会非常有帮助。同时,根据您的代码,您似乎在 brand_familybrand 之间存在一对多关系,因此,如果您使用 ORM,您可能有导航属性brand.brand_family。在这种情况下,您的 LINQ 查询中不需要 join

    var viewModel = new BrandIndexViewModel
                    {
                        BrandIndexItems = 
                           (from brand in brandDB.brands
                            orderby brand.brand_family.brand_family_name,
                                    brand.priority,
                                    brand.genre_id
                            select new BrandIndexItem
                            {
                                FamilyName = brand.brand_family.brand_family_name,
                                Priority = brand.priority,
                                RollImage = brand.roll_image,
                                Brands = brand.name // ?
                            }).ToList()
                    }

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 like brand.Name). Posting your brand and brand_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 group FamilyName, Priority, RollImage and Brand into one object and have a list of those in your ViewModel:

public class BrandIndexViewModel
{
    public List<BrandIndexItem> BrandIndexItems { get; set; }
}

public class BrandIndexItem
{
    public string Priority { get; set; }
    public string FamilyName { get; set; }
    public string RollImage { get; set; }
    public string Brand { get; set; }
}

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:

    var viewModel = new BrandIndexViewModel
                    {
                        BrandIndexItems = 
                           (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 BrandIndexItem
                            {
                                FamilyName = brand_family.brand_family_name,
                                Priority = brand.priority,
                                RollImage = brand.roll_image,
                                Brands = brand.name // ?
                            }).ToList()
                    }    

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 and brand, so if you are using an ORM, you probably have a navigational property brand.brand_family. In that case, you won't need a join in your LINQ query:

    var viewModel = new BrandIndexViewModel
                    {
                        BrandIndexItems = 
                           (from brand in brandDB.brands
                            orderby brand.brand_family.brand_family_name,
                                    brand.priority,
                                    brand.genre_id
                            select new BrandIndexItem
                            {
                                FamilyName = brand.brand_family.brand_family_name,
                                Priority = brand.priority,
                                RollImage = brand.roll_image,
                                Brands = brand.name // ?
                            }).ToList()
                    }
诗化ㄋ丶相逢 2024-11-08 06:28:41

我假设您正在使用 ASP.NET MVC。

要从您的视图访问 Brandslist 变量,请使用模型参数从控制器将其传入。例如:

ActionResult ListAllBrands()
{
    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
                         };

    return View("List", 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:

ActionResult ListAllBrands()
{
    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
                         };

    return View("List", brandslist);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文