asp.net-mvc - asp mvc 使用 View 模型在 View 中列出产品详细信息

标签 asp.net-mvc asp.net-mvc-4 viewmodel asp.net-mvc-viewmodel

我正在尝试在 View 中列出单个产品详细信息。产品规范是动态变化的,因为规范是在表格中逐行添加的,这意味着我们可以为每个产品添加大量规范(就像在电子商务网站中所做的那样)。现在我可以使用 ViewBag 来满足要求,但我决定使用 ViewModel作为更好的做法。

模型类:

// Product:
public partial class ProductTable
{
    public ProductTable()
    {
        this.SpecificationsTable = new HashSet<SpecificationsTable>();
    }

    public int ProductID { get; set; }
    public string Title { get; set; }
    public string SmallDescription { get; set; }
    public string FullDescription { get; set; }

    public virtual ICollection<SpecificationsTable> SpecificationsTable { get; set; }
    }

//Specifications:
public partial class SpecificationsTable
{
    public int SpecificationsID { get; set; }
    public string SpecificationName { get; set; }
    public string SpecificationValue { get; set; }
    public Nullable<int> ProductID { get; set; }
    public virtual ProductTable ProductTable { get; set; }
}

View 模型:
public class DetailsViewModel
{
    public int ProductID { get; set; }
    public string Title { get; set; }
    public string SmallDescription { get; set; }
    public string FullDescription { get; set; }
    public string SpecificationName { get; set; }
    public string SpecificationValue { get; set; }
}

操作方法
public ActionResult ProductDetails(int id)
{
    var details = (from c in dbo.ProductTable
                   join s in dbo.SpecificationsTable 
                   on c.ProductID equals s.ProductID
                   where c.ProductID == id
                   select new DetailViewModel
                   {
                       Title = c.Title,
                       SmallDescription = c.SmallDescription,
                       FullDescription = c.FullDescription
                   }).ToList();

     // To remove repeated product title , small and full description
     var distinctItems = details.GroupBy(x => x.ProductID).Select(y => y.First());

     // To show product title, small and full description for this product

     ViewBag.ProductDetails = distinctItems;

     var specifications = (from c in dbo.ProductTable
                             join s in dbo.SpecificationsTable 
                             on c.ProductID equals s.ProductID
                             where c.ProductID == id
                             select new DetailViewModel
                             {
                                 SpecificationName = s.SpecificationName,
                                 SpecificationValue = s.SpecificationValue
                             }).ToList();

    // To show list of specifications for this product
    ViewBag.Specifcations = specifications;
    return View();
}

预期输出:

详情:
Title: New Samsung offer

SmallDescription : Something small 

FullDescription : Something full

规范:
Mobile Name :Samsung

Model : 2015

Price : 70 $

Color:  White

我正在使用数据库优先方法,我正在尝试学习如何在这里使用 View 模型。

最佳答案

您当前的 View 模型没有反射(reflect)您想要在 View 中显示的内容,这是每个产品的多个规范,因此您需要一个集合属性。将您的 View 模型更改为

public class SpecificationVM
{
  public string Name { get; set; }
  public string Value { get; set; }
}
public class ProductVM
{
  public string Title { get; set; }
  public string SmallDescription { get; set; }
  public string FullDescription { get; set; }
  IEnumerable<SpecificationVM> Specifications { get; set; }
}

然后在 Controller 中,使用填充您的 View 模型
public ActionResult ProductDetails(int id)
{
  var product = db.ProductTable.Where(p => p.ProductID == id).FirstOrDefault();
  // note you may need to add .Include("SpecificationsTable") in the above
  if (product == null)
  { 
    return new HttpNotFoundResult();
  }
  ProductVM model = new ProductVM()
  {
    Title = product.Title,
    SmallDescription = product.SmallDescription,
    FullDescription = product.FullDescription,
    Specifications = product.SpecificationsTable.Select(s => new SpecificationVM()
    {
      Name = s.SpecificationName,
      Value = s.SpecificationValue
    })
  };
  return View(model);
}

然后在 View 中
@model yourAssembly.ProductVM
<h2>Details</h2>
@Html.DisplayNameFor(m => m.Title)
@Html.DisplayFor(m => m.Title)
.... // ditto for SmallDescription and FullDescription 
<h2>Specifications</h2>
@foreach(var item in Model.Specifications)
{
  @Html.DisplayFor(m => item.Name)
  @Html.DisplayFor(m => item.Value)
}

关于asp.net-mvc - asp mvc 使用 View 模型在 View 中列出产品详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31854913/

相关文章:

javascript - Json 字符串转 Google map 标记

asp.net-mvc - 从区域中删除 Controller 名称的 MVC5 自定义路由

html - 如何将 min 和 max 属性添加到 EditorFor 输入元素?

asp.net - ASP.NET MVC 5 中的自定义模型绑定(bind)器

.net - 设置每个操作的 MaxAllowedContentLength/maxRequestLength 值

html - 将 HTML 加载到页面中,首先获取为文本

c# - 从复杂对象中获取有序列表的最佳方法是什么,其中列表元素来自第二代子集合

MVVM/WPF : Using a ObservableCollection<T> as a list in a domain model, 是好还是坏?

asp.net-mvc - 哪一层应该构造一个 View 模型?

c# - 这是MVVM不好的做法吗?