Question

I'm pretty new to MVC and I'm trying to pull data from 8 different tables (1 record from each table but several fields from each record in each table)

I read how to do this with 2 tables and joining, but when I try to join more than two tables I get errors.

Each table has an ID field in each record and that is what I need to query against.

UPDATE:

Here is my ViewModel:

public class StoreInfoViewModel
    {
        public List<AWS_Ticket> tickets { get; set; }
        public List<AWS_TNF_StepOne> tnf { get; set; }
        public List<AWS_TNF_StepTwo> tnf2 { get; set; }
        public List<AWS_DBU> dbu { get; set; }
        public List<GetStoreInfoResult> storeInfo { get; set; }
    }

Here is my controller:

public class StoreInfoController : Controller
    {
        PortalDataContext db = new PortalDataContext();
        //
        // GET: /StoreInfo/
        public ActionResult Index(int id)
        {
            var tickets = db.AWS_Tickets.Where(x => x.Store_Number == id);
            var tnf = db.AWS_TNF_StepOnes.Where(x => x.Store_Number == id);
            var tnf2 = db.AWS_TNF_StepTwos.Where(x => x.Store_Number == id);
            var dbu = db.AWS_DBUs.Where(x => x.StoreNumber == id);
            var storeInfo = db.GetStoreInfo(id);

            StoreInfoViewModel model = new StoreInfoViewModel();
            model.tickets = tickets.ToList();
            model.tnf = tnf.ToList();
            model.tnf2 = tnf2.ToList();
            model.dbu = dbu.ToList();
            model.storeInfo = storeInfo.ToList();

            return View(model);
        }

Here's my View:

@model IEnumerable<Team_Portal_v3.Models.StoreInfoViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


<div>
    <table>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    Store Number: @Html.DisplayFor(modelItem => item.storeInfo[0].StoreNum)
                </td>
                <td>&nbsp;</td>
                <td>24 Hour Store? @Html.DisplayFor(modelItem => item.storeInfo[0].Is24Hour)</td>
            </tr>
            <tr>
                <td>
                    City / State: @Html.DisplayFor(modelItem => item.storeInfo[0].City) , @Html.DisplayFor(modelItem => item.storeInfo[0].State)
                </td>
                <td>
                    Store Phone: @Html.DisplayFor(modelItem => item.storeInfo[0].StorePhone)
                </td>
                <td>
                    DBU Installed? @Html.DisplayFor(modelItem => item.storeInfo[0].DBUInstalled)
                </td>
            </tr>
            <tr>
                <td>
                    Owner: @Html.DisplayFor(modelItem => item.storeInfo[0].Owner)
                </td>
                <td>
                    Owner Phone: @Html.DisplayFor(modelItem => item.storeInfo[0].OwnerPhone)
                </td>
                <td>
                    Store Status: @Html.DisplayFor(modelItem => item.storeInfo[0].StoreStatus)
                </td>
            </tr>
            <tr>
                <td>
                    Division: @Html.DisplayFor(modelItem => item.storeInfo[0].Division)
                </td>
                <td>
                    Region: @Html.DisplayFor(modelItem => item.storeInfo[0].Region)
                </td>
                <td>
                    Expected Opening Date: @Html.DisplayFor(modelItem => item.storeInfo[0].OpeningDate)
                </td>
            </tr>
        }
    </table>
</div>

I am not receiving any error on init running of the app, it when I try to go to the index with and numeric ID that I get the following error:

The model item passed into the dictionary is of type 'Team_Portal_v3.Models.StoreInfoViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Team_Portal_v3.Models.StoreInfoViewModel]'.

Where did I go wrong?

Was it helpful?

Solution

The error message is pretty self-explanatory here. You passed in a single StoreInfoViewModel but your view was expecting an IEnumberable<StoreInfoViewModel>.

Either your view needs to be updated to only expect a single StoreInfoViewModel

@model Team_Portal_v3.Models.StoreInfoViewModel

or you need to actually pass in a list of StoreInfoViewModel from your Controller/Action

OTHER TIPS

I don't want to be unpopular but you are better off using stored procedure when you cross 3 joins. Here is article on how to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top