【问题标题】:asp.net mvc 5 how to display data in table headerasp.net mvc 5 如何在表头中显示数据
【发布时间】:2016-09-23 11:29:01
【问题描述】:

我正在用 asp.net mvc 5 开发一个调度器。

我是新手,

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

这是我的数据库

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

用户22b登录时,在表头显示“手术一” & “手术二”

这是我的桌子

这是我的查看代码

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

    }

【问题讨论】:

  • 为什么 pplz 做负面标记,我陷入了这个问题

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


【解决方案1】:

这可以工作

控制器

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>

【讨论】:

  • “/”应用程序中的服务器错误。 'System.Collections.Generic.List.Count' 不能像方法一样使用。详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:不可调用的成员 'System.Collections.Generic.List.Count' 不能像方法一样使用。源错误:第 75 行:第 76 行: 第 77 行:@if (ViewBag.Headers != null && ViewBag.Headers.Count() > 0) 第 78 行: { 第 79 行:for (int i = 0; i
  • 这样,它会显示数据库中 schedule 表中的数据,我想显示数据库中 Surgeries 表中的数据,因为“schedule”不包含 SurgeryName,它包含“SurgeryID” "
  • 然后把 schedule.Select 换成 Surgery.Select (或者你想用的型号)
  • 面临一个小问题,如果我们有 2 条记录,它会显示 2 条记录,我怎样才能将列的限制固定为 6,剩下的 4 列应该是空的......如图我分享
  • 你能解释一下视图模型吗?请
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2018-01-14
  • 1970-01-01
相关资源
最近更新 更多