Asp.net MVC。如何在视图页面中显示名称而不是ID? [英] Asp.net MVC . How to display name instead of ID in view page?

查看:138
本文介绍了Asp.net MVC。如何在视图页面中显示名称而不是ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello People!

我对我的观点有疑问。

说明:我有一个包含多个表的数据库,与主键和辅助键有关。

桌子:

文章

类别

子类别



我要做的是,显示一个不同类别的产品列表。

但它不是一个名称,而是显示ID。

像这样:

子类别ID = 1

Libelle =三星s4

描述 =黑色,白色,金色

价格 = 75,00

股票 = 35


请点击显示我所看到内容的链接:

链接



而不是SubCategory Id,我希望它显示名称例如:智能手机,洗衣机,微波炉...



我尝试过:



型号 - > ArticleModels.cs

 public class ArticleModels 
{
public int ART_Id {get;组; }

[显示(名称=Sous分类)]
public int ART_SCAT_Id {get;组; }

[显示(名称=Libelle)]
公共字符串ART_Libelle {get;组; }

[显示(名称=描述)]
公共字符串ART_Description {get;组; }

[显示(名称=Prix)]
公共小数ART_Prix {get;组; }
[显示(名称=股票)]
public int ART_Stock {get;组; }
}





DAL - > ArticleBL.cs

 public static List< ArticleModels> SelectAllArticle()
{
var rtn = new List< ArticleModels>();
using(var context = new WebShopEntities())
{
// ReSharper禁用一次LoopCanBeConvertedToQuery
foreach(var item in context.Article)
{
rtn.Add(新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
});
}

}
返回rtn;
}





控制器 - > ArticleControllers.cs



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





这是我的表子类别

 public int SCAT_Id {get;组; } 
public int SCAT_CAT_Id {get;组; }
public string SCAT_Libelle {get;组; }

解决方案

如果您在上面显示的Model类是映射到您的数据库的Entity Model类,这很清楚从你的代码中,你需要创建一个ViewModel类并在你的视图中使用它,所以首先创建一个ViewModel:



 < span class =code-keyword> public   class  ArticleViewModel 
{
public int ART_Id { get ; set ; }

[显示(名称= Sous Categorie)]
public string SCAT_Libelle { get ; set ; }


[显示(名称= Libelle) ]
public string ART_Libelle { get ; set ; }

[显示(名称= 描述)]
public string ART_Description { get ; set ; }

[显示(名称= Prix)]
public decimal ART_Prix { get ; set ; }
[显示(名称= 股票)]
public int ART_Stock { get ; set ; }
}





然后在您的控制器操作中更改您的查询以在两个表之间建立连接 ART_SCAT_Id 并将结果投影到新创建ViewModel的类型列表中,在您需要更新方法定义的情况下:



  public   static 列表< ArticleViewModel> SelectAllArticle()
{
var articles = new 列表< ArticleViewModel>() ;
使用 var context = new WebShopEntities())
{
var result = 来自 article context中的class =code-keyword>。文章
join subCategory in context.subCategory
on article.ART_SCAT_Id等于subCategory.SCAT_CAT_Id
foreach var item 结果)
{
rtn.Add( new ArticleViewModel
{
ART_Id = article.ART_Id,
SCAT_Libelle = subCategory.SCAT_Libell e,
ART_Libelle = article.ART_Libelle,
ART_Description = article.ART_Description,
ART_Prix = article.ART_Prix,
ART_Stock = article.ART_Stock
});
}

}
返回条;
}





希望有所帮助。


Hello @Ehsan Sajjad,

感谢你的帮助。



我到达这样解决:

 var lstArt = new List< ArticleModels>(); 
使用(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();

}
返回lstArt;





公共类ArticleModels 
{
public int ART_Id {get;组; }
public int ART_SCAT_Id {get;组; }
[显示(名称=Libelle)]
公共字符串ART_Libelle {get;组; }
[显示(名称=描述)]
公共字符串ART_Description {get;组; }
[显示(名称=Prix)]
公共小数ART_Prix {get;组; }
[显示(名称=股票)]
public int ART_Stock {get;组; }
[显示(名称=Sous分类)]
公共字符串SCAT_Libelle {get;组; }
}





它的确有效! :)


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

解决方案

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:

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:

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.


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! :)


这篇关于Asp.net MVC。如何在视图页面中显示名称而不是ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆