-1

i am developing a scheduler in asp.net mvc 5.

i am new to it,

my question is, how i can display data on table header from my sql server database

here is my data base enter image description here

i want when user 6a loging, it display the "Blue Surgery" and "Grey surgery" in table header

when user 22b login, it display the "Surgery One" & "surgery Two" in the table header

here is my table enter image description here

here is my View code

 <tr>
                    <th style="width:50px;"></th>

                    <th id="header1">Surgery 1</th>

                    <th id="header2">Surgery 2</th>

                    <th id="header3"></th>

                    <th id="header4"></th>

                    <th id="header5"></th>

                    <th id="header6"></th>

                </tr>

here is my controller code

  // GET: Schedules/Create
    public ActionResult Create()
    {
        var currentUser = User.Identity.GetUserId();
        var schedule = db.AspNetUsers.Where(x => x.ClinicName == currentUser);
        ViewBag.CurrentUser = currentUser;

        //ViewBag.ClinicName = new SelectList(db.AspNetUsers, "Id", "FirstName");

        var doctor = db.Doctors.ToList().Where(d => d.ClinicName == currentUser);
        ViewBag.DoctorID = new SelectList(doctor, "DoctorID", "DoctorName");

        var surgery = db.Surgeries.ToList().Where(d => d.ClinicName == currentUser);
        ViewBag.SurgeryID = new SelectList(surgery, "SurgeryID", "SurgeryName");


        var patient = db.Patients.ToList().Where(p => p.ClinicName == currentUser);
        ViewBag.PatientID = new SelectList(patient, "PatientID", "PatientName");

        return View();
    }

my Index in Controller

// GET: Schedules
    public ActionResult Index()
    {

        //var schedules = db.Schedules.Include(s => s.AspNetUser).Include(s => s.Doctor).Include(s => s.Surgery).Include(s => s.Patient);
        //return View(schedules.ToList());
        var currentUser = User.Identity.GetUserId();
        var schedule = db.Schedules.Where(x => x.ClinicName == currentUser);


        return View(schedule);

        var surgery = db.Surgeries.Where(x => x.ClinicName == currentUser);
        ViewBag.Headers = surgery.Select(s => s.SurgeryName).ToList();

    }
Muhammad Naveed
  • 35
  • 1
  • 11

1 Answers1

1

This could work

Controller

ViewBag.Headers = schedule.Select(s => s.SurgeryName).ToList();

Html

<tr>
    <th style="width:50px;"></th>
    @if (ViewBag.Headers != null && ViewBag.Headers.Count > 0){
        for (int i = 0; i < ViewBag.Headers.Count; i++){
            <th id="header_@i">@ViewBag.Headers[i]</th>
        }
    }
</tr>

This will fix your empty column Problem, but you should really work with Viewmodels

 <tr>
     <th style="width:50px;"></th>
     @if (ViewBag.Headers != null && ViewBag.Headers.Count > 0){
         for (int i = 0; i < 6; i++){
             <th id="header_@i">@(ViewBag.Headers.Count > i ? ViewBag.Headers[i] : "")</th>
         }
     }
 </tr>
Bene
  • 95
  • 8
  • Server Error in '/' Application. 'System.Collections.Generic.List.Count' cannot be used like a method. Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Non-invocable member 'System.Collections.Generic.List.Count' cannot be used like a method. Source Error: Line 75: Line 76: Line 77: @if (ViewBag.Headers != null && ViewBag.Headers.Count() > 0) Line 78: { Line 79: for (int i = 0; i < ViewBag.Headers.Count(); i++) – Muhammad Naveed Sep 23 '16 at 12:10
  • with this, it display the data from schedule table in the database, i want to display the data from Surgeries table in the database, because "schedule " does not contains the SurgeryName, it contains the "SurgeryID" – Muhammad Naveed Sep 23 '16 at 12:40
  • Then replace schedule.Select with surgery.Select (Or the model you want to use) – Bene Sep 23 '16 at 12:43
  • facing a little problem, if we have 2 records, it displays 2 records, how i can fix the limit of columns to 6, remaining 4 columns should be empty......like the picture i share – Muhammad Naveed Sep 23 '16 at 14:35
  • can you explain regarding viewmodel? please – Muhammad Naveed Sep 23 '16 at 22:32
  • [A very good explanation of Viewmodel](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). – Bene Sep 26 '16 at 06:05