1

我试图从我的索引中传递一个额外的值,并且我已经从返回中传递了一些其他的东西,因为我使用的是 pagedList 方法。我需要将 vm 从索引返回中传递出来,所以它看起来像

return View(vm);

目前看起来像这样

return View(IAMP.ToPagedList(pageNumber, pageSize));

我一直在阅读下面链接的这个问题,以更好地了解如何将两个表传递给一个视图,但我无法弄清楚我应该如何使用 pagedList 所需的当前返回值返回“vm”。我还发布了我的控制器、模型和索引的代码,如果需要更多代码以提高清晰度,请告诉我。

MVC - 将多个数据表传递给视图

真正的问题是我需要获得一个版本号以显示在索引中以及位于另一个表中的其他值。我希望我正在尝试做的事情是有意义的。:) 感谢您对所有可以提供帮助的人的支持!

控制器

namespace DBFirstMVC.Controllers

{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();

        // Index Method 
        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            PageViewModel vm =  new PageViewModel();

            vm.iamp_mapping = from n in db.iamp_mapping select n;
            vm.version_number = from k in db.version_number select k;

            ViewBag.CurrentSort = sortOrder; //ViewBag property provides the view with the current sort order
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "PA desc" : ""; // Calls the sortOrder switch/case PA desc or default 
            ViewBag.MPSortParm = sortOrder == "MP" ? "MP desc" : "MP asc"; // Calls the sortOrder switch/case MP desc or MP asc
            ViewBag.IASortParm = sortOrder == "IA" ? "IA desc" : "IA asc"; // Calls the sortOrder switch/case IA desc or IA asc
            ViewBag.VersionSortParm = sortOrder == "VERSION" ? "Version desc" : "Version asc"; // Calls the sortOrder switch/case Version desc or Version asc
            ViewBag.IAMP_PKSortParm = sortOrder == "IAMP_PK" ? "IAMP_PK desc" : "IAMP_PK asc"; // Calls the sortOrder switch/case IAMP_PK desc or IAMP_PK asc

            if (Request.HttpMethod == "GET") 
            {
                searchString = currentFilter; //sets the currentFilter equal to Searchstring
            }
            else
            {
                page = 1;                   // defaults to page 1
            }
            ViewBag.CurrentFilter = searchString; // Provides the view with the current filter string


            var IAMP = from p in db.iamp_mapping select p;
            var VIAMP = from x in db.version_number select x;

            if (!String.IsNullOrEmpty(searchString))
            {
                IAMP = IAMP.Where(p => p.PA.ToUpper().Contains(searchString.ToUpper())); //selects only records that contains the search string
            }

            switch (sortOrder) // switch case changes based on desired sort 
            {
                case "Pa desc":
                    IAMP = IAMP.OrderByDescending(p => p.PA);
                    break;
                case "MP desc":
                    IAMP = IAMP.OrderByDescending(p =>p.MAJOR_PROGRAM);
                    break;
                case "MP asc":
                    IAMP = IAMP.OrderBy(p =>p.MAJOR_PROGRAM);
                    break;
                case "IA desc":
                    IAMP = IAMP.OrderByDescending(p => p.INVESTMENT_AREA);
                    break;
                case "IA asc":
                    IAMP = IAMP.OrderBy(p => p.INVESTMENT_AREA);
                    break;
                case "Version asc":
                    VIAMP = VIAMP.OrderBy(x => x.VERSION);

                    break;
                case "Version desc":
                    VIAMP = VIAMP.OrderByDescending(x => x.VERSION);
                    break;
                case "IAMP_PK asc":
                    IAMP = IAMP.OrderBy(p => p.IAMP_PK);
                    break;
                case "IAMP_PK desc":
                    IAMP = IAMP.OrderByDescending(p => p.IAMP_PK);
                    break;
                default:
                    IAMP = IAMP.OrderBy(p => p.PA);
                    break;
            }
            int pageSize = 15; // number of records shown
            int pageNumber = (page ?? 1); // start page number

            return View(IAMP.ToPagedList(pageNumber, pageSize)); // uses pagedList method to return correct page values
        }

PaDb模型

using System;
using System.Collections.Generic;
using System.Linq;
using DBFirstMVC.Models;

namespace DbFirstMVC.Models
{
    public class PageViewModel
    {
        public IQueryable<iamp_mapping> iamp_mapping { get; set; }
        public IQueryable<version_number> version_number { get; set; }

    }

}

指数

@model DBFirstMVC.Models.iamp_mapping

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>



@using (Html.BeginForm()) {

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>iamp_mapping</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.PA)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PA)
            @Html.ValidationMessageFor(model => model.PA)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.MAJOR_PROGRAM)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MAJOR_PROGRAM)
            @Html.ValidationMessageFor(model => model.MAJOR_PROGRAM)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.INVESTMENT_AREA)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.INVESTMENT_AREA)
            @Html.ValidationMessageFor(model => model.INVESTMENT_AREA)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VERSION)
        </div>
        <div class="editor-field">

            @Html.DropDownList("Version", ViewBag.Version as MultiSelectList)
            @Html.ValidationMessageFor(model => model.VERSION)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IAMP_PK)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IAMP_PK)
            @Html.ValidationMessageFor(model => model.IAMP_PK)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "")
</div>
4

1 回答 1

1

在您的 ViewModel 中公开您需要传递给视图的列表

var vm = new MyViewModel();

//do your stuff

vm.MyList1 = IAMP.ToPagedList(pageNumber, pageSize);
vm.MyList2 = VIAMP.ToPagedList(pageNumber, pageSize);
return View(vm);

然后在您看来,您将 foreach 用于每个列表或您需要做的任何事情

@model DBFirstMVC.Models.MyViewModel

@foreach(var item in Model.MyList1) {

}

@foreach(var item in Model.MyList2) {

}
于 2012-04-12T15:24:25.393 回答