在视图(.net MVC)中使用不同模型的属性 [英] Use properties of different models in view (.net MVC)

查看:83
本文介绍了在视图(.net MVC)中使用不同模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习MVC,并在视图中显示产品列表.

I am learning MVC and display a list of products in a view.

@model IEnumerable<Domain.Model.Product>

<table>
    <tr>
        <th style="width:50px; text-align:left">Id</th>
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Category</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.Name)
        </td>            
    </tr>
}

</table>

产品属于类别,在右列中显示.现在,我想按类别过滤产品,为此,我想使用一个下拉列表控件.我发现了@ Html.DropDownListFor(),但是据我了解,这只会为我提供当前基础模型(产品)的属性.

The products belong to categories, which are displayed in the right column. I now want to filter the products by categories, for which I would like to use a dropdownlist control. I found @Html.DropDownListFor(), but as far as I understand, this will only give me properties of the currently underlying model (Product).

我的控制器:

public class ProductController : Controller
{
    ProductRepository pr = new ProductRepository();

    public ActionResult Default()
    {
        List<Product> products = pr.GetAll();

        return View("List", products);
    }
}

推荐答案

您可以执行以下操作.只需使用所需的信息创建一个类.

You could do something like this. Just create a class with the info that you need.

public class ProductsModel 
{
     public ProductsModel() {
          products = new List<Product>();
          categories = new List<SelectListItem>();
     }

     public List<Product> products { get;set; }
     public List<SelectListItem> categories { get;set; }

     public int CategoryID { get;set; }

}

然后是您的控制器:

public class ProductController : Controller
{
    ProductRepository pr = new ProductRepository();

    public ActionResult Default()
    {
        ProductsModel model = new ProductsModel();
        model.products = pr.getAll();

        List<Category> categories = pr.getCategories();
        model.categories = (from c in categories select new SelectListItem {
            Text = c.Name,
            Value = c.CategoryID
        }).ToList();

        return View("List", model);
    }
}

最后,您的观点

@model IEnumerable<Domain.Model.ProductsModel>

@Html.DropDownListFor(m => model.CategoryID, model.categories)

<table>
    <tr>
        <th style="width:50px; text-align:left">Id</th>
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Category</th>
    </tr>

@foreach (var item in Model.products) {
    <tr>
        <td>
            @item.Id
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.Category.Name
        </td>            
    </tr>
}

</table>

这篇关于在视图(.net MVC)中使用不同模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆