简体   繁体   中英

Using 2 @model in View - ASP.Net MVC Bootstrap

I have a View that gets the model directly from a database table. I use the @model IEnumerable<TestMVC.USR_MSTR> to display data in <table> . Right now, I'm trying to implement table pagination I've read from a tutorial ( @model PagedList.IPagedList<CDS.USR_GRP_MSTR> ). It uses the same model with different type, so right now I'm having problems how to attach the same model with different type in my View.

I've read that I should create a ViewModel that calls the models I need but I'm quite new to MVC and still get confused in some areas. I've created a UserViewModel for that but not sure if I implemented it correctly, because in my View I cannot get the columns of my USR_MSTR model so I revereted back to my posted code below.

View (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) - This is where I get confused, did I implemented it correctly? I just get the 2 @models in my view and put it here so that this is the only one my view should call

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

    }
}

If you are using the PagedList as your Model , you do not have to pass the original IEnumerable . Just pass the paged list as your model.

@model IPagedList<CDS.USR_MSTR>

Then in your view code, just use it like IEnumerable :

@foreach(var item in Model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM