1

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>
Community
  • 1
  • 1
Goldentp
  • 187
  • 1
  • 7
  • 28

1 Answers1

1

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) {

}
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Interesting I am trying it out now I will let you know how it goes in a few minutes Thanks upvote for you – Goldentp Apr 12 '12 at 15:33
  • Ok... so I don't think I understand what you are trying to do. I tried putting `var vm = new PageviewModel` into the PaDbModels.cs class but VS doesn't like that very much threw an error // ERROR A namespace cannot directly contain members such as fields or methods ERROR.// I am probably misunderstanding what you are trying to say cause I'm new to MVC programming. Thank you for your response just the same! – Goldentp Apr 12 '12 at 15:46
  • In your `models` folder you create your ViewModel while in the controller you create an instance of it and map it with the data taken from your repositories. Than you pass the newly created viewmodel to the View. You shuold define MyList1 and MyList2 of the right type (IList/IEnumerable I suppose). Hope it helps – Iridio Apr 12 '12 at 15:55
  • You could also use the dynamic viewbag: `ViewBag.MyList1 = IAMP.ToPagedList(...)` so that you don't need to create a separate `MyViewModel` class – Troy Apr 19 '12 at 12:18