Francesco Bigi

Francesco Bigi

  • 1.6k
  • 57
  • 81.5k

Asp.net MVC . How to display name instead of ID in view page

Feb 4 2017 12:32 PM
Hello People!
I don't know how to show the the name instead of the ID

As you can see on the sub category, it is showing me the id result instead of the.
I have a table related with another table, on my sql db.
I made a connection string to my MVC project.
These are my codes:
Models -> ArticleModels.cs
 
 
  1. public class ArticleModels  
  2.     {  
  3.         public int ART_Id { getset; }  
  4.   
  5.         [Display(Name = "Sous Categorie")]  
  6.         public int ART_SCAT_Id { getset; }  
  7.   
  8.         [Display(Name = "Libelle")]  
  9.         public string ART_Libelle { getset; }  
  10.   
  11.         [Display(Name = "Description")]  
  12.         public string ART_Description { getset; }  
  13.   
  14.         [Display(Name="Prix")]  
  15.         public decimal ART_Prix { getset; }  
  16.         [Display(Name = "Stock")]  
  17.         public int ART_Stock { getset; }  
  18.     } 
 DAL -> ArticleBL.cs
 
  1. public static List SelectAllArticle()  
  2.        {  
  3.            var rtn = new List();  
  4.            using (var context = new WebShopEntities())  
  5.            {  
  6.                // ReSharper disable once LoopCanBeConvertedToQuery  
  7.                foreach (var item in context.Article)  
  8.                {  
  9.                    rtn.Add(new ArticleModels  
  10.                    {  
  11.                        ART_Id = item.ART_Id,  
  12.                        ART_SCAT_Id = item.ART_SCAT_Id,  
  13.                        ART_Libelle = item.ART_Libelle,  
  14.                        ART_Description = item.ART_Description,  
  15.                        ART_Prix = item.ART_Prix,  
  16.                        ART_Stock = item.ART_Stock  
  17.                    });  
  18.                }  
  19.   
  20.            }  
  21.            return rtn;  
  22.        }
 Controllers -> ArticleControllers.cs
  1. public ActionResult Index()  
  2.         {  
  3.             var lstClient = ArticleBL.SelectAllArticle().ToList();  
  4.             return View(lstClient);  
  5.         } 
 Views -> Article -> Index.cshtml
 
  1. @foreach (var item in Model)  
  2.    {  
  3.        <tr>  
  4.            <td>  
  5.                @Html.DisplayFor(modelItem => item.ART_SCAT_Id)  
  6.            td>  
  7.            <td>  
  8.                @Html.DisplayFor(modelItem => item.ART_Libelle)  
  9.            td>  
  10.            <td>  
  11.                @Html.DisplayFor(modelItem => item.ART_Description)  
  12.            td>  
  13.            <td>  
  14.                  
  15.                @Html.DisplayFor(modelItem => item.ART_Prix)  
  16.                <span class="glyphicon glyphicon-eur">span>  
  17.            td>  
  18.            <td>  
  19.                @Html.DisplayFor(modelItem => item.ART_Stock)  
  20.            td>  
  21.            <td>  
  22.                @Html.ActionLink("Details", "Details", new { id=item.ART_Id  }) |  
  23.                @Html.ActionLink("Buy", "Edit", null, new {@class = "btn btn-warning glyphicon glyphicon-shopping-cart"})  
  24.            td>  
  25.        tr>  
  26.    } 
 
 These are my tables:
 
 
 

Answers (6)