1

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

3
  • What you have so far is fine so I suspect the issue is how you are calling viewdata in the view. Commented May 31, 2012 at 14:11
  • This is MVC 3 and as far as a view goes I haven't had any successful attempts so far so there currently isn't anything. Commented May 31, 2012 at 14:15
  • @RichardK - I've updated my answer to show a way of rendering the view with the ViewModel. Commented Jun 1, 2012 at 15:51

3 Answers 3

2

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>
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is probably a better solution than mine if you'll be using that kind of model more than once. If you're just using it once, this is probably a little bit of overkill.
I like this one and am trying it now. This is more familiar to me. I will let everyone know how it turns out.
I have the modified code finished but I am unable to post it because of formatingI will read the help and see if I can figure it out. Also it is still not working. I am getting the following error message: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType48[System.Nullable1[System.Int64],System.String,System.String,System.String,System.String,System.Nullable1[System.Int64],System.Nullable1[System.Int64],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[CMS.Models.BizSurveyCVM]'.
It sounds like you're not doing the select new BizSurveyCVM within your Linq statement.
LINQ STATEMENT: 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, Address = Mstr.ADDRESS, NipaKey = Mstr.NIPAKEY, Date = Dat.SURVEYDATE, SurveyId = Dat.ID, Resurvey = Dat.RESURVEYOF, DateStamp = Dat.STAMP }).ToList();
|
0
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.

2 Comments

Since this is a join what model type would you use or should the join somehow be a model?
maybe a linked list or dictionary? You can pretty much use whatever type you want, so long as you handle the conversion first. If you wanted to do the dictionary route, youd loop through the join and build a Dictionary<something, something> out of it. Then pass the dictionary to the view and have the view accept dictionaries
0

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>

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.