Solved

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

Posted on 2013-01-17
2
7,749 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
Comment Utility
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
Comment Utility
Thanks masterpass, It worked
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now