且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ASP.NET MVC视图中访问模型的属性

更新时间:2023-10-07 13:42:04

看起来您正在将ProductConfiguration类对象的集合传递给视图.确保从操作方法向视图发送ProductBasket类的单个对象

Looks like you are passing a collection of ProductConfiguration class objects to the view. Make sure you are sending a single object of ProductBasket class to your view from your action method

public ActionResult Something()
{
  var vm = new ProductBasket();

 //The below line is hard coded for demo. You may replace with actual list from db.
  vm.Products.Add(new ProductConfiguration { Id=1,Name="test"});

  return View(vm);
}

假设您的ProductConfiguration类具有Id和Name属性.

Assuming your ProductConfiguration class has an Id and Name property.

您的视图应绑定到ProductBasket

@model YourNameSpaceHere.ProductBasket
<h1>Products</h1>

@foreach(var product in Model.Products)
{
  <h2>@product.Name</h2>
}