How do I pass a value out of the index controller in MVC3?

I am trying to pass an extra value out of my index and I am already passing a few other things out of the return because I am using a pagedList method. I need to pass vm out of the index return so it looks like

return View(vm);

at the moment it looks like this

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

I have been reading this question linked below to better understand how to pass two tables to one view but I can't figure out how I should return the "vm" with the current return values that are needed for pagedList. I have also posted my code for my controller, model and index, please let me know if any more code is needed for better clarity.

MVC - Passing multiple data tables to a view

The real problem is that I need to get a version number to display in the index as well as other values that are located in another table. I hope what I am trying to do makes sense. :) Thanks for you help upvotes to all those that can help!!

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; }

    }

}
Index
@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>

Answers


in your ViewModel expose the lists that you need to pass to the view

var vm = new MyViewModel();

//do your stuff

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

Then in your view you use foreach for every list or whatever you need to do

@model DBFirstMVC.Models.MyViewModel

@foreach(var item in Model.MyList1) {

}

@foreach(var item in Model.MyList2) {

}

Need Your Help

Creating a scheduling / calendar grid in ASP.NET

asp.net calendar

I am working on the prototype for a scheduling application on an intranet system. The application is for scheduling and tracking promotional workers at various locations on various dates.

handling an event in a running thread

c++ multithreading events boost

I have a function f1 which is running in a thread, and it continuously do a calculation c1.