如何手动添加其他模型进行查看?

时间:2016-07-21 15:15:09

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

当我创建名为Products的视图时,我通过视图生成器窗口指定了模型产品,并使用该模型显示数据库中的产品

@foreach (var item in Model)
    {
        <div class="col-md-3 col-sm-3 col-column productbox">
            <img src="@Url.Content(item.ProductImg)" class="img-responsive" >
            <div class="producttitle">@Html.DisplayFor(modelItem => item.ProductName)</div>
            <div class="producttitle">@Html.DisplayFor(modelItem => item.ProductDesc)</div>
            <div class="productprice"><div class="pull-right"><a href="#" class="btn btn-danger btn-sm" role="button">BUY</a></div><div class="pricetext">@Html.DisplayFor(modelItem => item.ProductPrice)</div></div>
        </div>

    }

现在我想在与动作链接相同的视图上生成数据库中的类别列表,因此当用户点击时,它会显示该类别的产品......类似这样的

<div class="col-md-2">
    <ul>

@foreach (var item in Model)
{
    <li>
        @Html.ActionLink(@Html.DisplayFor(modelItem => item.CategoryName).ToString(),"Products",new { controller="Home",action="Products",id=modelItem=>item.CategoryID})
    </li>

}
    </ul>
</div>

但我需要在视图中包含某种类型的模型,就像包含在产品

中一样
@model IEnumerable<OnlineProdajaSlika.Models.Product>

我是mvc新手并且无法弄明白,任何帮助都表示赞赏

2 个答案:

答案 0 :(得分:3)

您可以创建特定于此视图的新视图模型。

public class ProductsAndCategoriesVm
{
  public IEnumerable<Product> Products {set;get;}
  public IEnumerable<Category> Categories {set;get;}
  public ProductsAndCategoriesVm()
  {
    Products = new List<Product>();
    Categories = new List<Category>();
  }
}

并在你的GET Action中创建一个对象,分配属性值并将其发送到视图

public ActionResult Index()
{
  var vm = new ProductsAndCategoriesVm();
  // Set vm.Products
  //  vm.Products=dbContext.Products.ToList();
  // Set vm.Categories
  //  vm.Categories=dbContext.Categories.ToList();
  return View(vm);
}

现在,您的视图应该强烈输入此视图模型

@model ProductsAndCategoriesVm
<h1>Products</h1>
@foreach(var p in Model.Products)
{
  <p>@p.ProductName</p>
}
<h1>Categories </h1>
@foreach(var c in Model.Categories)
{
  <p>@Html.ActionLink(c.CategoryName,"Products","Home",new { @id=c.CategoryID},null)</p>
}

答案 1 :(得分:2)

我认为您所追求的是具有一个或多个属性的视图模型,例如:

public class ProductsViewModel
{
    // Property to hold your products
    public IEnumerable<Product> Products { get; set; }

    // Property to hold the category details
    public IEnumerable<Category> Categories { get; set; }
}

public class Category
{
    public string CategoryName { get; set; }
}

public class Product
{
    public string ProductName { get; set; }
}

然后,您可以在控制器操作中填充这些详细信息并返回此模型:

public MyController : Controller
{
    public ActionResult ProductsPage()
    {
        var viewModel = new ProductsViewModel
        {
            Products = new List<Product>(),
            Categories = new List<Category>()
        };

        // Populate the products
        viewModel.Products.Add(new Product { ProductName = "Product 1" });
        viewModel.Products.Add(new Product { ProductName = "Product 2" });

        // Populate the categories on the same view model
        viewModel.Categories.Add(new Category { CategoryName = "Category 1" });
        viewModel.Categories.Add(new Category { CategoryName = "Category 2" });

        // Return the view model with all of the details
        return View(viewModel);
    }
}

然后在视图中定义此视图模型并循环数据:

@model ProductsViewModel

@* Loop the products in the model *@
@foreach(var product in Model.Products)
{
    <div>@product.ProductName</div>
}

@* Loop the categories in the model *@
@foreach (var category in Model.Categories)
{
    <div>@category.CategoryName</div>
}