如何使用mvc5的javascript计时器?

时间:2017-11-19 05:24:52

标签: javascript timer asp.net-mvc-5

我有一个DropDownList,其中包含患者列表和一张表格,该表格在同一视图中显示所选患者的生命体征(页面)。我使用JavaScript设置计时器,以便DropDownList和表都可以每5秒刷新一次。但它只刷新DropDownList。当我在下拉列表中点击患者姓名时,我收到了这样的错误

  

" EmployeeListItems =' model.EmployeeListItems'抛出了类型' System.ArgumentNullException'"和对象引用未设置为对象的实例

这是我的观点

@model eVitalz_mvc.Models.PatientlistModel
....
 <script type="text/javascript">
    setInterval(function ()
    {
        $.post("@Url.Action("ListPatients", "Home")", {}, function () { 
        });
    }, 5000)
 </script>

<table cellpadding="4" cellspacing="4">
    <tr>
        <td>
            Patient list:
        </td>
        <td>
            @using (Html.BeginForm("ListPatients", "Home",FormMethod.Post, new { id = "demoForm", name = "demoForm" }))
            {
                @Html.DropDownListFor(m => m.Preg_id, Model.EmployeeListItems, "---Select---", new { Class = "ddlStyle", onchange = "SelectedIndexChanged()" })
            }
        </td>
    </tr>
</table>

<h1 style="color: #2D3F43; font-weight: bold; font-size: 20px; text-align: center">Glucose</h1>
<table align="center">
    <tr>
        <td rowspan="2">
            <img id="ContentPlaceHolder1_Image_Glucose" src="~/images-old-evitalz/blood1.png" style="height: 100px; width: auto" />
        </td>
        <td></td>
        <td>
            <span id="ContentPlaceHolder1_lbl_glucose_unit">@Html.DisplayFor(model => model.Glucose) mmol</span>
        </td>
    </tr>
</table>

这是我的模特课

public class PatientlistModel
{
    public static string s = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
    SqlConnection con = new SqlConnection(s);
    DataTable dt = new DataTable();
    public List<PatientlistModel> Patients { get; set; }
    public int Preg_id { get; set; }
    public string P_Name { get; set; }
    public string  age { get; set; }
    public string temperature { get; set; }
    public string Glucose { get; set; }
    public string Pressure_SYS { get; set; }
    public string Pressure_DIA { get; set; }
    public string Pressure_PR { get; set; }
    public string Oximeter_SPO2 { get; set; }
    public string Oximeter_PR { get; set; }
    public string ECG { get; set; }
    public IEnumerable<SelectListItem> EmployeeListItems
    {
        get
        {
            return new SelectList(Patients, "Preg_id", "P_Name");
        }
    }
}

这是我的控制器动作

public ActionResult ListPatients()
{
    PatientlistModel pt = new PatientlistModel();
    int id = Convert.ToInt32(System.Web.HttpContext.Current.Session["uid"]);
    pt.Patients = PopulatePatientList(id);
    return View(pt);
}

[HttpPost]
public ActionResult ListPatients(PatientlistModel model)
{
    int s = Convert.ToInt32(model.Preg_id);
    System.Web.HttpContext.Current.Session["pid"] = s;
    int id = Convert.ToInt32(System.Web.HttpContext.Current.Session["uid"]);
    PatientlistModel obj = new PatientlistModel();
    obj.Patients = PopulatePatientList(Convert.ToInt32(id));
    //sample code to get glucose
    obj.Glucose = emp1.Rows[m]["D_Value1"].ToString();
    return View(obj);
}

0 个答案:

没有答案