Click here to Skip to main content
15,944,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i am new to asp.net MVC so please dont judge me,

I have a model

C#
namespace WebApplication1.Models
{
    public class productDB
    {
        public int Producttype { get; set; }
        public string Productquantity { get; set; }


i have this calculation in my controller:

C#
public ActionResult prog()
        {
            var TypeQty = db.productDB.GroupBy(fu => fu.Producttype)
            .Select(g => new { Name = g.Key, Count = g.Count() });
            return TypeQty;
//running into a problem here aswell "Cannot imlicitly convert type"

        }


How do i pass the result of calculation to my view? thanks for your help!

the names of object in my original code is not in english, there might be inconsistencies due to quick rewrite to english
Posted
Updated 22-Aug-15 9:44am
v4

1 solution

You should create a concrete class with a Name and Count property and create a list of those

C#
var TypeQty = db.productDB.GroupBy(fu => fu.Producttype)
            .Select(g => new YourClassNameHere{ Name = g.Key, Count = g.Count() });


Your model will now be

@model IEnumerable<yourclassnamehere>
 
Share this answer
 
v2
Comments
Member 11926599 22-Aug-15 16:02pm    
Should i create that class in the controller or model?
Afzaal Ahmad Zeeshan 22-Aug-15 16:15pm    
Anywhere, make sure you do not miss the namespace it is created in. :-)
F-ES Sitecore 22-Aug-15 16:48pm    
Create it in the Models folder.
Member 11926599 23-Aug-15 7:17am    
hello its me again, i have tried pretty much everything and still couldnt get it to work, seems like im missing something and i can't find a answer for it, when i do db.productDB.GroupBy i get error "db." does not exist in current context
F-ES Sitecore 23-Aug-15 8:07am    
You need to create the database context before you can use it. Go through this tutorial, it shows you everything from start to finish

http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900