如何将2个列表传递给1个视图

时间:2014-05-03 12:22:59

标签: list asp.net-mvc-4 view controller webgrid

我想在我的视图中显示最近和传入的约会(一页)。这是我的控制器,我可以在哪里添加第二个列表,如何将其传递到同一视图?我知道我不能返回两个列表但是必须是为了吗?

public ActionResult Index()
        {
            if (Session["UserEmail"] != null) 
            {
                string Email = (string)Session["UserEmail"];

                using (var db = new MaindbModelDataContext())
                {
                    var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);
                    ViewBag.FirstName = patient.Name;
                    ViewBag.LastName = patient.Surname;
                    ViewBag.BirthDate = patient.Birthday;
                    ViewBag.Email = patient.Email;


                }


                using (var db = new MaindbModelDataContext())
                {
                    var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);
                    var listrecent = (from y in db.Appointments
                                      where y.PatientNo == patient.PatientNo
                                      where y.Date < DateTime.Today
                                      orderby y.Date descending
                                      select y).Take(5); 

                    var TempRecent = new List<Models.AppModel>();
                    foreach (var item in listrecent)
                    {
                        var Temp = new Models.AppModel();
                        Temp.AppNo = item.AppNo;
                        Temp.PatientNo = (Int32)item.PatientNo;
                        Temp.Date = (DateTime)item.Date;
                        Temp.Status = item.Status;
                        Temp.Description = item.Description;
                        TempRecent.Add(Temp);

                    }
                    return View(TempRecent);
                }

            }
            else
            {
                return RedirectToAction("RegAndLogin", "User");
            }
        }

    }
}

这是我的观点部分

@model IEnumerable<DentAppSys.Models.AppModel>
@using System.Web.Helpers

@{
    ViewBag.Title = "Index";
}


    <section class="Patient-Dashboard">

        <div id="dashboard_left">
            <h1> Recent Appointments</h1>
   @{
    var Mygrid = new WebGrid(Model, selectionFieldName: "SelectedRow");
}

@Mygrid.GetHtml(
    displayHeader: true,

    mode: WebGridPagerModes.FirstLast,

columns: Mygrid.Columns
(

    Mygrid.Column("Appointment No", "Appointment No",format: @<text>@item.AppNo</text>),
    Mygrid.Column("Patient No", "Patient No", format: @<text>@item.PatientNo</text>) ,
    Mygrid.Column("Description", "Description", format: @<text>@item.Description</text>),
    Mygrid.Column("Date", "Date", format: @<text>@item.Date.ToString("yyyy/MM/dd")</text>),
    Mygrid.Column("Status", "Status", format: @<text>@item.Status</text>)

))
</div>

<div id="dashboard_right">
<br/>
<h1>Incoming Appointments</h1>

  /* HERE I WANT TO ADD MY SECOND LIST*/
</div>
</section>

修改

并且在使用AppModel的两个实例后,当我尝试等于Temp.RecentIncoming.AppNo = item.AppNo时,我收到错误。

using (var db = new MaindbModelDataContext())
                {
                    var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);
                    var listincoming = (from y in db.Appointments
                                      where y.PatientNo == patient.PatientNo
                                      where y.Date > DateTime.Today
                                      orderby y.Date descending
                                      select y).Take(5);

                    var TempIncoming = new List<Models.RecentIncoming>();
                    foreach (var item in listincoming)
                    {
                        var Temp = new Models.RecentIncoming.;
                        Temp.RecentIncoming.AppNo?????= item.AppNo;
                        Temp.PatientNo = (Int32)item.PatientNo;
                        Temp.Date = (DateTime)item.Date;
                        Temp.Status = item.Status;
                        Temp.Description = item.Description;
                        TempIncoming.Add(Temp);

                    }
                    return View(TempIncoming);
                }

1 个答案:

答案 0 :(得分:0)

不要将IEnumerable作为模型,而是创建一个新的模型类,它包含两个AppModel实例以及您需要传递给视图的任何其他附加数据......

public class MyAppointments
{
   public IEnumerable<DentAppSys.Models.AppModel> RecentAppts;
   public IEnumerable<DentAppSys.Models.AppModel> IncomingAppts;
   public MyAppointments() { }
}
...
return View( new MyAppointments() {  RecentAppts=TempRecent, IncomingAppts=TempIncoming       } );

将视图更改为...

    @model MyAppointments
    ...
   @{
    var MyRecentgrid = new WebGrid(Model.RecentAppts, selectionFieldName: "SelectedRow");
    var MyIncomingGrid = new WebGrid(Model.IncomingAppts, selectionFieldName: "SelectedRow");
    }