在视图中使用2 @model - ASP.Net MVC Bootstrap

时间:2016-07-21 03:45:24

标签: c# asp.net-mvc view model asp.net-mvc-5

我有一个直接从数据库表中获取模型的View。我使用@model IEnumerable<TestMVC.USR_MSTR><table>中显示数据。现在,我正在尝试实现表格分页,我已从教程(@model PagedList.IPagedList<CDS.USR_GRP_MSTR>)中读取。它使用不同类型的相同模型,所以现在我遇到了如何在我的视图中使用不同类型附加相同模型的问题。

我已经读过我应该创建一个调用我需要的模型的ViewModel,但我对MVC很新,但在某些方面仍然感到困惑。我为此创建了一个UserViewModel,但不确定我是否正确实现了它,因为在我的View中我无法获取USR_MSTR模型的列,所以我虔诚地回到下面发布的代码。

查看(Index.cshtml)

@model IEnumerable<TestMVC.USR_MSTR>
@*@model PagedList.IPagedList<CDS.USR_GRP_MSTR>*@
@using PagedList.Mvc;

@using (Html.BeginForm("Index", "User", FormMethod.Get))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="container">
        @Html.DropDownList("UserFilter")
        <table id="dbTable" onclick="gettabledata('dbTable','dbBody','0')" class="table table-scroll table-condensed table-hover table-striped bootgrid-table" aria-busy="false">
            <thead>
                <tr>
                    <th class="hidden">
                        @Html.DisplayNameFor(model => model.USR_ID)
                    </th>
                    <th>
                        @Html.ActionLink("Users", "Index", new { sortOrder = ViewBag.UserSort, currentFilter = ViewBag.CurrentFilter })
                        @*This is to create a link that sorts the Users in descending*@
                    </th>
                </tr>
            </thead>
            <tbody id="dbBody">
                @foreach (var item in Model)
                {
                    <tr>
                        <td class="hidden">
                            @Html.DisplayFor(modelItem => item.USR_ID)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.USR_DESC)
                        </td>
                    </tr>
                }
            </tbody>
        </table>

    </div>
}

Controller(UserController.cs)

using TestMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace MVCTest.Controllers
{
    public class UserController : Controller
    {
    MVCTestEntities _db = new MVCTestEntities();

        // GET: User
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.UserSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSort = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var users = from u in _db.USR_MSTR
                            select u;

            if (!String.IsNullOrEmpty(searchString))
            {
                users = users.Where(s => s.USR_DESC.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "name_desc":
                    users = users.OrderByDescending(ug => u.USR_DESC);
                    break;
                case "Date":
                    users = users.OrderBy(ug => u.CREA_DT);
                    break;
                case "date_desc":
                    users = users.OrderByDescending(ug => u.CREA_DT);
                    break;
                default:
                    users = users.OrderBy(ug => u.USR_DESC);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            var filters = GetUserGroupFilter();
            ViewBag.UserGroupFilter = GetSelectListItems(filters);

            return View(users.ToPagedList(pageNumber, pageSize));
        }
    }
}

ModelView(UserViewModel.cs) - 这是我感到困惑的地方,我是否正确实施了它?我只是在我的视图中获得2个@models并将其放在这里,以便这是我的视图应该调用的唯一一个

namespace TestMVC.ViewModels
{
    public class UserViewModel
    {
        public IEnumerable<CDS.USR_MSTR> table { get; set; }
        public PagedList.IPagedList<CDS.USR_MSTR> pagination { get; set; }

    }
}

1 个答案:

答案 0 :(得分:2)

如果您使用ngInit作为PagedList,则无需传递原始Model。只需将分页列表作为模型传递。

IEnumerable

然后在您的视图代码中,只需使用@model IPagedList<CDS.USR_MSTR>

IEnumerable