开发者

How do I break up my data so I can feed it through to my view?

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 vie开发者_如何学JAVAw 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; }

}


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
        };


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()
                    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜