Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello People!
I have in issue with my Views.
Explain: I have a db with several tables, related with primary and secondary keys.
Tables:
Article
Category
SubCategory

What I am trying to do is, show a list of products with different category.
But instead of a the name it shows me the ID.
Like this:
Sub Category Id = 1
Libelle = Samsung s4
Description = black, white, gold
Price = 75,00
Stock = 35

Please click on the link that show what I am seeing:
Link

Instead of SubCategory Id, I would like that it show the Name, example: Smartphone, washing machine, microwave ...

What I have tried:

Models -> ArticleModels.cs
public class ArticleModels
    {
        public int ART_Id { get; set; }

        [Display(Name = "Sous Categorie")]
        public int ART_SCAT_Id { get; set; }

        [Display(Name = "Libelle")]
        public string ART_Libelle { get; set; }

        [Display(Name = "Description")]
        public string ART_Description { get; set; }

        [Display(Name="Prix")]
        public decimal ART_Prix { get; set; }
        [Display(Name = "Stock")]
        public int ART_Stock { get; set; }
    }


DAL -> ArticleBL.cs
public static List<ArticleModels> SelectAllArticle()
        {
            var rtn = new List<ArticleModels>();
            using (var context = new WebShopEntities())
            {
                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var item in context.Article)
                {
                    rtn.Add(new ArticleModels
                    {
                        ART_Id = item.ART_Id,
                        ART_SCAT_Id = item.ART_SCAT_Id,
                        ART_Libelle = item.ART_Libelle,
                        ART_Description = item.ART_Description,
                        ART_Prix = item.ART_Prix,
                        ART_Stock = item.ART_Stock
                    });
                }

            }
            return rtn;
        }


Controllers -> ArticleControllers.cs

public ActionResult Index()
        {
            var lstClient = ArticleBL.SelectAllArticle().ToList();
            return View(lstClient);
        }


This is my table subCategory
public int SCAT_Id { get; set; }
public int SCAT_CAT_Id { get; set; }
public string SCAT_Libelle { get; set; }
Posted
Updated 6-Feb-17 21:31pm

If the Model class which you have shown above is the Entity Model class which is mapped to your database, which is quite clear from your code, you would need to create a ViewModel class and use that in your view, so create a ViewModel first:

C#
public class ArticleViewModel
{
    public int ART_Id { get; set; }

    [Display(Name = "Sous Categorie")]
    public string SCAT_Libelle { get; set; }


    [Display(Name = "Libelle")]
    public string ART_Libelle { get; set; }

    [Display(Name = "Description")]
    public string ART_Description { get; set; }

    [Display(Name="Prix")]
    public decimal ART_Prix { get; set; }
    [Display(Name = "Stock")]
    public int ART_Stock { get; set; }
}


and then in your controller action change your query to have a join between both table on the ART_SCAT_Id and project the result in List of type your new create ViewModel, in your case you need to update the definition of method:

C#
public static List<ArticleViewModel> SelectAllArticle()
        {
            var articles = new List<ArticleViewModel>();
            using (var context = new WebShopEntities())
            {
                var result = from article in context.Article
                             join subCategory in context.subCategory
                             on article.ART_SCAT_Id equals subCategory.SCAT_CAT_Id   
                foreach (var item in result)
                {
                    rtn.Add(new ArticleViewModel
                    {
                        ART_Id = article.ART_Id,
                        SCAT_Libelle = subCategory.SCAT_Libelle,
                        ART_Libelle = article.ART_Libelle,
                        ART_Description = article.ART_Description,
                        ART_Prix = article.ART_Prix,
                        ART_Stock = article.ART_Stock
                    });
                }

            }
            return articles;
        }


Hope it helps.
 
Share this answer
 
v2
Hello @Ehsan Sajjad,
thanks for your help.

I Arrived to solve like this:
var lstArt = new List<ArticleModels>();
            using (var ctx = new WebShopEntities1())
            {
                lstArt = ctx.Article.Select(a => new ArticleModels()
                {
                    ART_Id = a.ART_Id,
                    ART_SCAT_Id = a.ART_Stock,
                    ART_Libelle = a.ART_Libelle,
                    ART_Description = a.ART_Description,
                    ART_Prix = a.ART_Prix,
                    ART_Stock = a.ART_Stock,
                    SCAT_Libelle = a.SousCategorie.SCAT_Libelle
                }).ToList();

            }
            return lstArt;


public class ArticleModels
    {
        public int ART_Id { get; set; }
        public int ART_SCAT_Id { get; set; }
        [Display(Name = "Libelle")]
        public string ART_Libelle { get; set; }
        [Display(Name = "Description")]
        public string ART_Description { get; set; }
        [Display(Name = "Prix")]
        public decimal ART_Prix { get; set; }
        [Display(Name = "Stock")]
        public int ART_Stock { get; set; }
        [Display(Name = "Sous Categorie")]
        public string SCAT_Libelle { get; set; }
    }


And it works! :)
 
Share this answer
 
Comments
Ehsan Sajjad 7-Feb-17 6:32am    
you should not be updating your model if it is your DTO class, better approach is to create a ViewModel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900