0

I have the following classes in my mvc model and I'm using EF

 public class Patient
    {
        [Key()]
        public int PatientId { get; set; }
        public string Name { get; set; }
     }

public class Treatment
    {
        [Key()]
        public int Id { get; set; }

        [ForeignKey("Patient")]
        public int PatientId { get; set; }
        public Patient Patient { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime TreatmentDate { get; set; }
}

I want to display the names of the Patients and in the same row the number of treatments they received.

The 2 solutions i can think of but looking for something more elegant:

  1. Iterating my Patients and for each count its treatments
  2. Group Treatments and count. Then Join between that result and Patient.

Thanks!

UshaP
  • 1,271
  • 2
  • 18
  • 32

3 Answers3

1
public class Patient
{
    [Key()]
    public int PatientId { get; set; }
    public string Name { get; set; }
    public ICollection<Treatment> Treatments
 }

In your view;

@foreach(var item in Model)
{
    <h1>@item.Name</h1>
    <h2>@item.Treatment.TreatmentDate
}

in your query;

db.EntityName.Select(p=> new Treatment()
{
    PatientId = p.PatientId,
    .....
...
    Treatments = p.Treatments
});
Idrees Khan
  • 7,702
  • 18
  • 63
  • 111
  • I don't understand your query. db is not enumerable so how do you "Select" and what is ViewModel? – UshaP Mar 17 '13 at 07:40
  • sorry, i missed the table name :D – Idrees Khan Mar 17 '13 at 07:55
  • viewmodel is just a name for your custom viewmodel e.g in your case it's Treatment. A viewmodel is a special kind for class that can pass data required to the view only – Idrees Khan Mar 17 '13 at 07:57
  • Sorry but it still doesn't make sense to me. I need to return to my view the patient object including the treatments. there are 2 things i'm confused about. 1. I have Treatments property in my Patient class and i don't understand why it isn't populated automatically. 2. In your suggestion - i return anonymous type and i'm not sure how to bind it to my view. – UshaP Mar 17 '13 at 08:31
  • see this http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3. I hope this helps :D – Idrees Khan Mar 18 '13 at 04:18
1

Finally i found the answer - and it is so simple !!!

db.Patients.Include("Treatments").ToList()

and then in my view i simply do

@Html.DisplayFor(modelItem => item.Treatments.Count)
UshaP
  • 1,271
  • 2
  • 18
  • 32
0

You should use ViewModel for your Views . I think this is better practice.Here you can read about it : What is ViewModel in MVC? . Basicly it's simple class with only this properties which you need for your Views. I don't understand your Model very well but I would do something like this

    public class Patient
    {

        public int Id { get; set; }// if it's Id it's default Primery Key
        public string FirstName { get; set; }
 public string LastName { get; set; }
     }

public class Treatment
    {

        public int Id { get; set; }// if it's Id it's default Primery Key


        public Patient IdPatient { get; set; } // Your Foreign key

        [DataType(DataType.DateTime)]
        public DateTime TreatmentDate { get; set; }
}

with this, one patient may have more then one TreatmentDate . I'm not pro just enthusiast. Good luck.

Community
  • 1
  • 1
Krasimir
  • 259
  • 4
  • 9
  • 28
  • thanks, but this is one to man. One patient can have many treatments. I need to display a grid with the patient data + number of treatments he received. – UshaP Mar 17 '13 at 08:37