如何将数据从控制器传递到mvc中的视图

Use*_*r26 0 asp.net-mvc entity-framework asp.net-mvc-4

  1. 我有问题从控制器发送数据到视图.

  2. 当我从控制器传递值到视图时,它变为null.

  3. 我想显示特定医生的预约细节.

例如,如果患者XYZ想要在特定时间和日期预约ABC医生,则所选医生只能查看预约的详细信息.患者也可以查看他的预约详细信息.

我有预约模特

 public partial class Appointment
{
    public int Id { get; set; }
    public int PatientId { get; set; }
    public int DoctorId { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public Nullable<System.DateTime> TimeOfDrAvailablity { get; set; }

    public virtual Doctor Doctor { get; set; }
    public virtual Patient Patient { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

医生控制器(我正在尝试到目前为止......在这里我得到了dr.ID和Patient.Id但是当它通过查看时它变为空)

  public ActionResult Appointment(Doctor doc)
    {

        var app=new Appointment();

        app.DoctorId = db.Doctors.FirstOrDefault().Id;
        app.PatientId = db.Patients.FirstOrDefault().Id;

        return View();
    }
Run Code Online (Sandbox Code Playgroud)

视图

   @using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form-horizontal role='form' " }))
   {
    <tr>
    <td>@Html.DisplayFor((model=>model.Appointments.FirstOrDefault().Date), new { @class = "Form-control" })
</td>                  
 <td>
 @Html.DisplayFor((model => model.Id), new { @class = " Form-control" })
  </td>                    
  <td>                        
 @Html.DisplayFor((model => model.Appointments.FirstOrDefault().PatientId), new { @class = " Form-control" })
 </td>                   
 <td> 
  <div class="btn-group">
   <div class="btn btn-Sucess">
 @Html.ActionLink("Edit", "Edit", new { id =Model.Id })
  </div>
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 5

您需要将模型传递给视图,如下所示

public ActionResult Appointment(Doctor doc)
{
    var app = new Appointment();

    [...]

    return View(app);
}
Run Code Online (Sandbox Code Playgroud)

您的视图然后引用您的模型如下

@model MyProject.Appointment

<p>Doctor: @Model.DoctorId</p>
<p>Patient: @Model.PatientId</p>
Run Code Online (Sandbox Code Playgroud)