4

I'm new to MVC and LINQ. Currently I faced difficulty on the project and decide to posted up.

My MVC-View that I wanted to achieve

Cut
----------------------------------
1   20%     
2   40%     
Color
----------------------------------
3   30%    
4   50%     
Perm
----------------------------------
5   10%     

This is some example of my data table

ID  Offer   Service
-------------------
1   20%     Cut
2   40%     Cut
3   30%     Color
4   50%     Color
5   10%     Perm

My Controller:

var services = (from ps in db.PS
                select ps).Distinct().ToArray();
ViewBag.services = services;

My View:

@foreach (var item in ViewBag.services){
    <h3 class="page-header">
        @item
    </h3>

    //Table TAG INSERT Here: ID, Offer, Service
}

PROBLEM comes now, I have no idea on how to populate the data from DB into the view according to their own Service(eg: Cut, Color, Perm) in View

I'm thinking of doing this to store the data according to services in my Controller:

foreach (var i in services){
    var servicesdata = (from ps in db.PS
                        where ps.Service == i
                        select ps).ToArray();
}

I'm wondering can I push the services data that already according to the services into some kind of array so that I can populate into view?

0

2 Answers 2

3

You can use a .GroupBy() clause to group your data by Service. Start by creating view models to represent what you want to display in the view

public class OfferVM
{
    public int ID { get; set; }
    [DisplayFormat(DataFormatString = "{0:P0}")]
    public float Offer { get; set; } // assumes you store this as float in the db
}
public class ServiceVM
{
    public string Name { get; set; }
    public IEnumerable<OfferVM> Offers { get; set; }
}

Then in the controller

IEnumerable<ServiceVM> model = db.PS.GroupBy(x => x.Service).Select(x => new ServiceVM()
{
    Name = x.Key,
    Offers = x.Select(y => new OfferVM()
    {
        ID = y.ID,
        Offer = y.Offer
    })
});
return View(model);

And in the view

@model IEnumerable<ServiceVM>
@foreach (var service in Model)
{
    <h2>@service.Name</h2>
    foreach (var item in service.Offers)
    {
        <span>@item.ID</span>
        <span>@Html.DisplayFor(m => item.Offer)</span>
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Classic group by question
Thank You so much @Stephen Muecke
1

ViewBag is a dynamic property you can insert ViewBag anything but when you retrieve data from ViewBag, first need to convert it.

@foreach (var item in (List<PS>)ViewBag.services){
    <h3 class="page-header">
        @item
    </h3>

    //Table TAG INSERT Here: ID, Offer, Service
}

2 Comments

Yeah understand, but can you elaborate more on the (List<PS>)ViewBag.Services ?
List<PS> PS is your business class first declare namespace on view to uses these class Ex @Using YourProjectName.WhereYourClassIs like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.