Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:53:43+00:00 2026-05-23T10:53:43+00:00

I am working on MVC3 asp.net. this is my sr=tatement in controller:- ViewBag.rawMaterialRequired =

  • 0

I am working on MVC3 asp.net.
this is my sr=tatement in controller:-

ViewBag.rawMaterialRequired = (from x in db.RawMaterial 
 join y in db.ProductFormulation on x.ID equals y.RawMaterialID 
 where y.ProductID == p select new { x.Description, y.Quantity });

this is my code in View:-

 @foreach(var album in ViewBag.rawMaterialRequired)
         {
                @album<br />
         }
         </div>

this is my output:-

{ Description = Polymer 26500, Quantity = 10 }
{ Description = Polymer LD-M50, Quantity = 10 }
{ Description = Titanium R-104, Quantity = 20 }

this is my desired output:-

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:53:44+00:00Added an answer on May 23, 2026 at 10:53 am

    Start by designing a view model:

    public class ProductLineViewModel
    {
        public string Description { get; set; }
        public int Quantity { get; set; }
    }
    

    then in your controller use this view model:

    ViewBag.rawMaterialRequired = 
         from x in db.RawMaterial 
         join y in db.ProductFormulation on x.ID equals y.RawMaterialID 
         where y.ProductID == p 
         select new ProductLineViewModel 
         { 
             Description = x.Description, 
             Quantity = y.Quantity 
         };
    

    and inside the view:

    <table>
        <thead>
            <tr>
                <th>Description</th>
                <th>Quantity</th>
            </tr>
        </thead>
        <tbody>
        @foreach(var product in (IEnumerable<ProductLineViewModel>)ViewBag.rawMaterialRequired)
        {
            <tr>
                <td>@product.Description</td>
                <td>@product.Quantity</td>
            </tr>
        }
        </tbody>
    </table>
    

    That’s was the first step into improving your code. The second step consists of getting rid of ViewBag and use strongly typed views and display templates:

    public ActionResult Foo()
    {
        var model = 
             from x in db.RawMaterial 
             join y in db.ProductFormulation on x.ID equals y.RawMaterialID 
             where y.ProductID == p 
             select new ProductLineViewModel 
             { 
                 Description = x.Description, 
                 Quantity = y.Quantity 
             };
        return View(model);
    }
    

    and in the view remove any ugly loops thanks to display templates:

    @model IEnumerable<ProductLineViewModel>
    <table>
        <thead>
            <tr>
                <th>Description</th>
                <th>Quantity</th>
            </tr>
        </thead>
        <tbody>
            @Html.DisplayForModel()
        </tbody>
    </table>
    

    and inside the display template (~/Views/Shared/DisplayTemplates/ProductLineViewModel.cshtml):

    @model ProductLineViewModel
    <tr>
        <td>@Html.DisplayFor(x => x.Description)</td>
        <td>@Html.DisplayFor(x => x.Quantity)</td>
    </tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a asp.net mvc3 application. I have this Jquery function function
I am working on asp.net mvc3 application. In this user can post data, i
I am working on asp.net mvc3 application and have many records coming from database.
I'm currently reading a book about ASP.NET MVC3 to learn working with this framework.
I'm working on ASP.net MVC3 Web application that is facing scalability issue. For improving
I'm working on Asp.net MVC3 web application. I'm having one page on which appx.
I'm working with ASP.NET MVC3, and I'm trying to get absolute control over my
I'm working on an ASP.NET website (MVC3 but not really important for my question)
ASP.NET MVC3 newb here. I am working on a MVC3 intranet application that uses
I'm working on ASP.NET MVC3. I was told by my senior to remove all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.