Richard K
Richard K

Reputation: 11

Passing Data from Controllers to Views

I have spent the past day and a half trying to find this answer and sometimes it seems like I am so close but so far no luck. I have a controller where I defined a LINQ query and am trying to figure out how to pass the results onto a listing view. the following is the Controller code:

namespace CMS.Controllers
{
    public class SurveyController : Controller
    {

        private SupportEntities supdb = new SupportEntities();
        private BuisnessEntities bsdb = new BuisnessEntities();

        //
        // GET: /Survey/BizSurveyC

        public ViewResult BizSurveyC(string nipaKey, string bOrg)
        {
            // use the next two lines for testing and then either delete or comment them out.
            nipaKey = "22";
            bOrg = "MPC";

            var Cquery = from Mstr in bsdb.BizOrgInsts
                                     join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                     where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                     orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                     select new { MasterId = Mstr.ID, Name = Mstr.OLDNAME, Mstr.ADDRESS, Mstr.NIPAKEY, Dat.SURVEYDATE, SurveyId = Dat.ID, Dat.RESURVEYOF, Dat.STAMP };

            //ViewBag.BizQuery = Cquery;
            ViewData["BizQuery"] = new SelectList(Cquery);

            return View();
        }

    }
}

As you can tell by looking I have tried ViewData and Viewbag but so far with no luck


Here are the way things are now appearing:

ViewModel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CMS.Models
{
    public class BizSurveyCVM
    {
        public long? MasterId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string NipaKey { get; set; }
        public string Date { get; set; }
        public long? SurveyId { get; set; }
        public long? Resurvey { get; set; }
        public string DateStamp { get; set; }
    }
}

Modified Action

        var Cquery = (from Mstr in bsdb.BizOrgInsts
                                    join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
                                    where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
                                    orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
                                    select new BizSurveyCVM
                                    {
                                        MasterId = Mstr.ID,
                                        Name = Mstr.OLDNAME,
                                        Address = Mstr.ADDRESS,
                                        NipaKey = Mstr.NIPAKEY,
                                        Date = Dat.SURVEYDATE,
                                        SurveyId = Dat.ID,
                                        Resurvey = Dat.RESURVEYOF,
                                        DateStamp = Dat.STAMP
                                    }).ToList();

        return View(Cquery);
    }

BizSurveyC View

@model List<CMS.Models.BizSurveyCVM>

<table>
    <tr>
        <th>
            MasterId
        </th>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            NipaKey
        </th>
        <th>
            Date
        </th>
        <th>
            SurveyId
        </th>
        <th>
            Resurvey
        </th>
        <th>
            DateStamp
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.MasterId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NipaKey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SurveyId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Resurvey)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateStamp)
        </td>
</table>

here is the resulting view:

SORRY NOT ALLOWED TO SAVE IMAGE YET BUT IN THE VIEW I HAVE HEADERS BUT NO DATA.

I obviously have some work to do in the view or maybe the query but things are looking much better thanks so everyone's help. Thank you very much

Upvotes: 1

Views: 400

Answers (3)

CAbbott
CAbbott

Reputation: 8098

You could create a ViewModel class to hold whatever your query results are and then strongly type your view for the new ViewModel class:

ViewModel Class:

public class BizSurveyCVM 
{
    public long MasterId { get; set; }
    public string Name { get; set; }
    ...
}

Modified Action:

var Cquery = (from Mstr in bsdb.BizOrgInsts
              join Dat in bsdb.BizSurveyQ on Mstr.ID equals Dat.MASTERID
              where Mstr.NIPAKEY == nipaKey & Mstr.FULCIRCORG == bOrg
              orderby Mstr.STREETSUFX, Dat.ADDRESS, Mstr.NUMBER
              select new BizSurveyCVM  { MasterId = Mstr.ID, Name = Mstr.OLDNAME, ...}
             ).ToList();

return View(Cquery);

BizSurveyC View

@model List<{namespace}.BizSurveyCVM>

@foreach(var item in Model) {
     {HTML Mark-up}
}

EDIT: Here's an updated example of the view based on the updated question:

@model List<{namespace}.BizSurveyCVM>

@foreach(var item in Model) {
    <tr>
        <td>
            @item.MasterId
        </td>
        <td>
            @item.Name
        </td>
        <td>
            @item.Address
        </td>
        ...
    </tr>
}

Upvotes: 2

Zach dev
Zach dev

Reputation: 1620

Are you sure that you want do?

it's more useful the collection in the view, so you can build anything you need, like a select list.

don't forget define who is your datavalue and displayvalue in the select list, like..

ViewData["BizQuery"] = new SelectList(Cquery, null, "MasterId", "Name");

Ok, your code works for me, so check your view, you must do the correct cast of your object in the viewdata

you need have something like this

@foreach (SelectListItem item in (SelectList)ViewData["BizQuery"])
{

    <p>@item.Text</p>

}

Upvotes: 0

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

return View(Cquery);

or

return View("ViewName",Cquery);

and then in your view, the model type should match the type of Cquery. But I find that its easier (assuming you're using VS) to just right click in the method body somewhere and click "add view", and select the model from the list of model types.

Upvotes: 0

Related Questions