Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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,661 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

610 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