asp.net mvc 5如何在表头中显示数据

时间:2016-09-23 11:29:01

标签: c# asp.net asp.net-mvc tableheader

我正在asp.net mvc 5中开发一个调度程序。

我是新手,

我的问题是,如何在我的sql server数据库中显示表头上的数据

这是我的数据库 enter image description here

我希望当用户6a登录时,它会在表格标题中显示“蓝色手术”“灰色手术”

当用户22b登录时,它会显示“Surgery One”&表格标题中“手术二”

这是我的桌子 enter image description here

这是我的查看代码

 <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>

这是我的控制器代码

  // 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();
    }

我在控制器中的索引

// 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();

    }

1 个答案:

答案 0 :(得分:0)

这可行吗

控制器

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>

这将解决您的空列问题,但您应该真正使用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>