?
Solved

How to Display Data in a single view from Multiple Tables in ASP.NET MVC 3.0?

Posted on 2013-01-17
2
Medium Priority
?
9,287 Views
Last Modified: 2013-01-18
Hello,

I'm new to asp.net mvc coding as this is my first project in asp.net,so im facing a lot of problems with it,my current problem is listed below

i need to get data from two table or two class in single view in ASP.NET MVC 3. I want to show data from both the tables in a single view,Below are my code ViewModel,Controller,View codes respectively


My ViewModel class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcJsp1.Models;

namespace MvcJsp1.ViewModels
{
    public class CustomerCartViewModel
    {
        public List<Order> OrderData { get; set; }
        public List<OrderDetail> OrderDetailData { get; set; }

      }
}

Open in new window



Controller code
 public ActionResult Details(int id)
        {

            CustomerCartViewModel orderView = new CustomerCartViewModel(); 

            orderView.OrderData = (from o in db.Orders select o).ToList();
            orderView.OrderDetailData = (from or in db.OrderDetails select or).ToList();

            return View(orderView);


            //Order order = db.Orders.Find(id);
            //return View(order);
         }

Open in new window



View
 
@model IEnumerable<MvcJsp1.Models.OrderViewData>
@{
    ViewBag.Title = "Details";
}

<style type="text/css">

    .display-label {

        width:180px;
        font-weight:bold;
        padding:5px;
    }

    .display-field {

         font-weight:normal;
        padding:5px 5px 10px 5px;
        text-transform:capitalize;
    }


</style>


<h2>Quote Details</h2>


<p>
     @Html.ActionLink("Back to List", "Index")
</p>

<table style="width:800px">
    <tr>
        <td width="30%" align="">
            
<fieldset>
    <legend>Customer Details</legend>

    <div class="display-label">FirstName - <span style="font-weight:normal">@Html.DisplayFor(model => model.FirstName) </span>   </div>

    <div class="display-label">LastName - <span style="font-weight:normal">@Html.DisplayFor(model => model.LastName) </span>    </div>

    <div class="display-label">Address - <span style="font-weight:normal">@Html.DisplayFor(model => model.Address)  </span>   </div>

    <div class="display-label">City - <span style="font-weight:normal">@Html.DisplayFor(model => model.City) </span>    </div>

    <div class="display-label">State - <span style="font-weight:normal">@Html.DisplayFor(model => model.State)    </span> </div>

    <div class="display-label">PostalCode - <span style="font-weight:normal">@Html.DisplayFor(model => model.PostalCode) </span>    </div>

    <div class="display-label">Country - <span style="font-weight:normal">@Html.DisplayFor(model => model.Country)  </span>   </div>

    <div class="display-label">Phone - <span style="font-weight:normal">@Html.DisplayFor(model => model.Phone)   </span>  </div>

    <div class="display-label">Email - <span style="font-weight:normal">@Html.DisplayFor(model => model.Email)   </span>  </div>
</fieldset>


        </td>
        
        <td width="50%" align="">
            
<fieldset>
    <legend>Cart Details</legend>

    <div class="display-label">FirstName - <span style="font-weight:normal">@Html.DisplayFor(model => model.FirstName) </span>   </div>

    <div class="display-label">LastName - <span style="font-weight:normal">@Html.DisplayFor(model => model.LastName) </span>    </div>

    <div class="display-label">Address - <span style="font-weight:normal">@Html.DisplayFor(model => model.Address)  </span>   </div>

    <div class="display-label">City - <span style="font-weight:normal">@Html.DisplayFor(model => model.City) </span>    </div>

    <div class="display-label">State - <span style="font-weight:normal">@Html.DisplayFor(model => model.State)    </span> </div>

    <div class="display-label">PostalCode - <span style="font-weight:normal">@Html.DisplayFor(model => model.PostalCode) </span>    </div>

    <div class="display-label">Country - <span style="font-weight:normal">@Html.DisplayFor(model => model.Country)  </span>   </div>

    <div class="display-label">Phone - <span style="font-weight:normal">@Html.DisplayFor(model => model.Phone)   </span>  </div>

    <div class="display-label">Email - <span style="font-weight:normal">@Html.DisplayFor(model => model.Email)   </span>  </div>
</fieldset>


        </td>


        <td align="center"></td>

    </tr>


</table>

Open in new window



Error
The error i'm getting is

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

Open in new window




Kindly help me, it is a very urgent delivery
0
Comment
Question by:SwiftCoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
masterpass earned 2000 total points
ID: 38788920
you need to pass the model as CustomerCartViewModel. so correct this line

@model IEnumerable<MvcJsp1.Models.OrderViewData>

to

@model MvcJsp1.Models.CustomerCartViewModel

hope this helps
0
 

Author Closing Comment

by:SwiftCoder
ID: 38792293
Thanks masterpass, It worked
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differeā€¦
Make the most of your online learning experience.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question