如何从MVC3中的索引控制器传递一个值?

时间:2012-04-12 15:12:11

标签: asp.net-mvc database asp.net-mvc-3 linq

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

return View(vm);

目前它看起来像这样

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

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

MVC - Passing multiple data tables to a view

真正的问题是我需要获取要在索引中显示的版本号以及位于另一个表中的其他值。我希望我想做的事情有道理。 :)感谢您帮助所有那些可以提供帮助的人!

PaController

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
        }

PaDbModel

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>

1 个答案:

答案 0 :(得分: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) {

}