Solved

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

Posted on 2013-01-17
2
8,391 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
2 Comments
 
LVL 21

Accepted Solution

by:
masterpass earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

860 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