如何在另一个表的View中显示Category Name而不是Category ID?

时间:2015-12-03 13:45:56

标签: c# sql asp.net-mvc

弄清楚它有什么问题,我怎样才能实现这个目标...... 有2个数据库表 - 类别和产品。

在我的Kategoria专栏中,我想要准确显示该类别的名称,而不是ID。在我的模型类下面。

Product.cs

public partial class Category
{
    public int ID { get; set; }
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public int ClientID { get; set; }
}

Category.cs

public partial class Product
{
    public int ID { get; set; }
    public int ProductID { get; set; }
    public string Name { get; set; }
    public int CategoryID { get; set; }
    public decimal Price { get; set; }
    public int Promotion { get; set; }
    public string Image1 { get; set; }
    public string Image2 { get; set; }
    public string Image3 { get; set; }
    public string Image4 { get; set; }
    public string Description { get; set; }
    public int ClientID { get; set; }

    public virtual Category Category { get; set; }
}

ViewPart:

@Html.DisplayFor(modelItem => item.CategoryID)

当我输入modelItem => item.Category.Name它不起作用(列为空)。任何想法我怎么能这样做?

控制器:

public class ProductsController : Controller
    {
        private ProductContext db = new ProductContext();

        private void LoadCreateViewBagData()
        {
            string domain = Request.Url.Host;
            int clientid = (from a in db.Client where a.Domain == domain select a.ID).First();

            int maxID = db.Product.Where(c => c.ClientID == clientid).Max(c => (int?)c.ProductID) ?? 0;

            ViewBag.MaxID = maxID + 1;

            IEnumerable<SelectListItem> categories = db.Category.Where(d => d.ClientID == clientid).Select(b => new SelectListItem { Text = b.Name, Value = b.CategoryID.ToString() });

            ViewData["categories"] = categories;

            ViewBag.ClientID = clientid;
        }

        // GET: Admin/Products
        public ActionResult Index()
        {
            string domain = Request.Url.Host;
            int clientid = (from a in db.Client where a.Domain == domain select a.ID).First();

            LoadCreateViewBagData();

            return View(db.Product.ToList().OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid));
        }

2 个答案:

答案 0 :(得分:1)

只需更改行

即可
return View(db.Product.Include(p =>p.Category).ToList().OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid));

然后下面的行将在视图中工作

@Html.DisplayFor(modelItem => item.Category.Name)

根据打击更改产品类

public partial class Category
 {
   public Category()
    {
      this.Products = new HashSet<Product>();
    }

    public int ID { get; set; }
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public int ClientID { get; set; }

   public virtual ICollection<Product> Products { get; set; }
}
public partial class Product
{
 public int ID { get; set; }
 public int ProductID { get; set; }
 public string Name { get; set; }
 public int CategoryID { get; set; }
 public decimal Price { get; set; }
 public int Promotion { get; set; }
 public string Image1 { get; set; }
 public string Image2 { get; set; }
 public string Image3 { get; set; }
 public string Image4 { get; set; }
 public string Description { get; set; }
 public int ClientID { get; set; } 

 public virtual Category Category { get; set; }
}

答案 1 :(得分:1)

试试这个:

@Html.DisplayFor(modelItem => item.Category.Name)

控制器:

public class ProductsController : Controller
{
    private ProductContext db = new ProductContext();

    private void LoadCreateViewBagData()
    {
        string domain = Request.Url.Host;
        int clientid = (from a in db.Client where a.Domain == domain select a.ID).First();

        int maxID = db.Product.Where(c => c.ClientID == clientid).Max(c => (int?)c.ProductID) ?? 0;

        ViewBag.MaxID = maxID + 1;

        IEnumerable<SelectListItem> categories = db.Category.Where(d => d.ClientID == clientid).Select(b => new SelectListItem { Text = b.Name, Value = b.CategoryID.ToString() });

        ViewData["categories"] = categories;

        ViewBag.ClientID = clientid;
    }

    // GET: Admin/Products
    public ActionResult Index()
    {
        string domain = Request.Url.Host;
        int clientid = (from a in db.Client where a.Domain == domain select a.ID).First();

        LoadCreateViewBagData();

        return View(db.Product.Include("Category").Where(c => c.ClientID == clientid).OrderByDescending(a => a.ProductID).ToList());
    }

提示以获得更好的效果

您在Where:

之前使用ToList
return View(db.Product.ToList().OrderByDescending(a => a.ProductID).Where(c => c.ClientID == clientid));

使用Where过滤然后使用ToList获取数据:

return View(db.Product.Include("Category").Where(c => c.ClientID == clientid).OrderByDescending(a => a.ProductID).ToList());