过滤多个ViewModel表

时间:2018-01-09 12:21:17

标签: c# asp.net-mvc razor mvvm foreach

我使用ViewModel显示多个表,每个表都有CompanyName,我试图将其循环播放,以便显示的数据是fore AddCustomersID,(AddCustomersID是关键对于包含CompanyName)的模型。这是为了向每个客户显示不同模态的信息,目前每个客户的数据都显示在每个模态上。

查看:

@model Intranet.ViewModels.IntranetViewModel
@foreach (var item in Model.AddCustomers)
{
<div class="modal">
                <h2>@Html.DisplayFor(modelItem => item.CompanyName)</h2>
<table>
    <tr>
        <th>Company Name</th>
        <th>Forename</th>
        <th>Surname</th>
        <th>Title</th>
        <th>Mobile</th>
        <th>Telephone</th>
        <th>Email</th>
    </tr>
    @foreach (var item in Model.EmployeeInfo)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.AddCustomers.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Forename)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Surname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mobile)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Telephone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
        </tr>
    }
</table>
<table>
    <tr>
        <th>Company Name</th>
        <th>Code</th>
        <th>Address</th>
        <th>Postcode</th>
        <th>Telephone</th>
    </tr>
    @foreach (var item in Model.ContactInfo)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.AddCustomers.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Code)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Postcode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Telephone)
            </td>
        </tr>
    }
</table>
...
</div>
}

视图模型:

namespace Intranet.ViewModels
{
    public class IntranetViewModel
    {
        public IntranetViewModel()
        {
            AddCustomers = new List<AddCustomers>();
            EmployeeInfo = new List<EmployeeInfo>();
            ContactInfo = new List<ContactInfo>();
...
        }

        public int IntranetViewModelID { get; set;}

        public IEnumerable<AddCustomers> AddCustomers { get; set; }
        public IEnumerable<EmployeeInfo> EmployeeInfo { get; set; }
        public IEnumerable<ContactInfo> ContactInfo { get; set; }
...    
    }
}

控制器:

private IntranetContext db = new IntranetContext();

        public ActionResult Index()
        {
            var viewModel = new IntranetViewModel
            {
                AddCustomers = db.AddCustomers,
                EmployeeInfo = db.EmployeeInfo,
                ContactInfo = db.ContactInfo,
...
            };

            return View(viewModel);
        }

我尝试使用Javascript过滤器,但这只适用于在单个表上使用getElementById,并且不能与getElementsByClassName一起使用以允许在多个表上使用。

我也尝试过使用@using (Html.BeginForm())下拉列表但是为此我还没有找到多个表的工作示例。

有没有办法在调用@foreach时使用c#razor进行过滤甚至更好的过滤?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚如何使用.Where<>执行此操作,例如:

<table>
    <tr>
        <th>Company Name</th>
        <th>Code</th>
        <th>Address</th>
        <th>Postcode</th>
        <th>Telephone</th>
    </tr>
    @foreach (var CIitem in Model.ContactInfo.Where(i => i.AddCustomers.AddCustomersID == item.AddCustomersID))
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => CIitem.AddCustomers.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => CIitem.Code)
            </td>
            <td>
                @Html.DisplayFor(modelItem => CIitem.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => CIitem.Postcode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => CIitem.Telephone)
            </td>
        </tr>
    }
</table>

这已指定仅显示已创建的模态的AddCustomersID