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,625 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.

733 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