gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:19:27 24 4
gpt4 key购买 nike

我正在尝试在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com