asp.net-mvc – asp mvc使用View Model在视图中列出产品详细信息
发布时间:2020-12-16 07:20:19 所属栏目:asp.Net 来源:网络整理
导读:我想在视图中列出单个产品的详细信息.产品规格会动态变化,因为规格是在表格中逐行添加的,这意味着我们可以为每种产品添加大量规格(如电子商务网站中所做的那样).现在我能够使用ViewBag满足要求,但我决定使用ViewModel作为更好的做法. 型号类: // Product:pu
我想在视图中列出单个产品的详细信息.产品规格会动态变化,因为规格是在表格中逐行添加的,这意味着我们可以为每种产品添加大量规格(如电子商务网站中所做的那样).现在我能够使用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; } } 视图模型: 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; } } ActionMethod 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 Specifcations: Mobile Name :Samsung Model : 2015 Price : 70 $ Color: White 我正在使用数据库优先方法,我正在尝试学习如何在这里使用视图模型. 解决方法
您当前的视图模型不反映您要在视图中显示的内容,这是每个产品的多个规范,因此您需要一个集合属性.将视图模型更改为
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; } } 然后在控制器中,使用填充查看模型 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); } 然后在视图中 @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 MVC3 – 如何从数据库加载页
- asp.net-mvc – MVC5 Microsoft.CSharp.RuntimeB
- asp.net-mvc-4 – mvc4 url??验证
- asp.net – asp文本框控件的CSS
- asp.net-mvc – ASP.NET MVC 5 Html.HiddenFor呈
- IdentityServer4身份认证授权入门-----客户端凭据
- asp.net – 服务器端检测页面显示在IFrame中
- asp.net-mvc – MVC架构 – 重新使用相同的viewm
- 如何在ASP.Net MVC中执行301永久重定向路由
- 具有匹配客户端和服务器端标记的ASP.NET页面的选
热点阅读